S.J S.J - 21 days ago 6
iOS Question

Why objective-c NS_ENUM variable automatically have default value

I created an enum like this

typedef NS_ENUM(NSInteger, PermissionStages) {
thePermissionNotDetermine = 0,
thePermissionDenied = 1,
theReminderPermissionAllowed = 2,
};


And create a variable like this

PermissionStages PermissionStageVar;


I have not assign any value to it, but by default this
variable
has
PermissionStages enum
first value, in this case its
thePermissionNotDetermine


Why is this behavior?

Answer

The line:

PermissionStages PermissionStageVar;

is getting a default value of 0. This is similar to the line:

NSString *foo;

resulting in foo having an initial value of nil. Or

BOOL aBool;

resulting in aBool having an initial value of NO.

The variables are all initialized to a value of "0".

Since your enum happens to have a value with 0, your variable appears to be initialized with thePermissionNotDetermine.