문제

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top