开发者

How do I pass an enum to a method in a subclass (from a ViewController)?

开发者 https://www.devze.com 2023-02-05 06:52 出处:网络
My SomeClass.h typedef enum thing {stuff, foo, bar, etc} thing; @interface SomeClass : UIView { NSNumber *aNumber;

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 thingor 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.

0

精彩评论

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

关注公众号