سؤال

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