How do I pass an enum to a method in a subclass (from a ViewController)?
My SomeClass.h
typedef enum thing {stuff, foo, bar, etc} thing;
@interface SomeClass : UIView {
NSNumber *aNumber;
thing aThing;
}
My SomethingViewController.m
-(void)doSomething:(NSNumber *)n
开发者_如何学C withThing:(id)t
{
SomeClass *class = [[SomeClass alloc] initWithFrame:aRect];
[class setAThing:(thing)t];
[class setANumber:n];
}
-(void)viewDidLoad
{
[self doSomething:[NSNumber numberWithInt:2] withThing:foo];
This seems to work fine, aside from a warning about the withThing: making a pointer from an integer without a cast.
I guess my question is: Am I doing this right? Is there a another way that will not give me this warning?
Yes there is another way. In an enum, all of your "things" are associated with a integer. So if you change the id
in your method to thing
or int
it should work without a warning. This is the way I do it all the time. ;-)
You can use the enumeration's name instead of id in your withThing: argument. Then you can remove the cast from t.
Also, I recommend using capitalized names for enumerations.
精彩评论