开发者

Is it possible to use a enum like UITableViewCellStyle as parameter of a method?

开发者 https://www.devze.com 2023-01-21 19:23 出处:网络
I want to have a enum as a parameter of my function. Would this work? (UIFont*) myMethodName:(UITableViewCellStyle) cellStyle开发者_如何学C {

I want to have a enum as a parameter of my function. Would this work?

(UIFont*) myMethodName:(UITableViewCellStyle) cellStyle开发者_如何学C {
    //...
    if (cellStyle == UITableViewCellStyleValue2)
        // ...
}

Then I would call the method like this way

UIFont *resultFont = [self myMethodName:UITableViewCellStyleSubtitle];

Only the following parameters should be allowed: UITableViewCellStyleDefault, UITableViewCellStyleValue1, UITableViewCellStyleValue2, UITableViewCellStyleSubtitle

Is it possible?


  • Would this work? → Yes

  • Only the following parameters should be allowed: → No it is not possible to restrict the input to just these values, i.e.

    UIFont *resultFont = [self myMethodName:12345];
    

    will still compile (assuming you are not using Objective-C++).


Sure:

typedef enum _MyType {
    type_a = -1,
    type_b = 0,
    type_c = 1,
} MyType;

...

- (void) someMethod:(MyType)type {
    if (type == type_a) ...
}


Yes, it's possible.

(This feels like an unnecessarily short answer but I can't think of anything else to add!)

0

精彩评论

暂无评论...
验证码 换一张
取 消