开发者

Default values of ivars with enum type in Objective-c

I have an enum like this:

typedef enum {
    ThingA,
    ThingB,
    ThingC
} MyType;

I have a class with an ivar declared in the interface like this:

@property (nonatomic) MyType myTypeIvar;

Currently the default value of myTypeIvar is ThingA (since ThingA is in the 0th position in the enum). What's the best way to make default value of myTypeIvar ThingB?

One solution is to simply re-order the enum definition (put ThingB 开发者_StackOverflow中文版in the 0th position). Another solution is to set myTypeIvar in the init method. However the init method doesn't exist (and when it's added it's never called).


What's the best way to make default value of myTypeIvar ThingB?

Set the value of myTypeIvar to ThingB in your designated initializer, i.e. the initializer that all other initializers are expected to call. For example, if the class in question is a subclass of UIViewController, you'd override -initWithNibName:bundle: because that's the designated initializer for UIViewController.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜