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!)
精彩评论