为什么Objective-C NS_ENUM变量自动具有默认值 [英] Why objective-c NS_ENUM variable automatically have default value
本文介绍了为什么Objective-C NS_ENUM变量自动具有默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了这样的枚举
typedef NS_ENUM(NSInteger, PermissionStages) {
thePermissionNotDetermine = 0,
thePermissionDenied = 1,
theReminderPermissionAllowed = 2,
};
并创建一个像这样的变量
And create a variable like this
PermissionStages PermissionStageVar;
我没有为其分配任何值,但是默认情况下,此变量
具有 PermissionStages枚举
的第一个值,在这种情况下,其是 thePermissionNotDetermine
I have not assign any value to it, but by default this variable
has PermissionStages enum
first value, in this case its thePermissionNotDetermine
为什么会这样?
推荐答案
该行:
PermissionStages PermissionStageVar;
的默认值为 0
.这类似于以下行:
is getting a default value of 0
. This is similar to the line:
NSString *foo;
导致 foo
的初始值为 nil
.或者
BOOL aBool;
产生的 aBool
的初始值为 NO
.
所有变量都初始化为"0"值.
The variables are all initialized to a value of "0".
由于您的枚举值恰好具有 0
的值,因此您的变量似乎已使用 thePermissionNotDetermine
进行了初始化.
Since your enum happens to have a value with 0
, your variable appears to be initialized with thePermissionNotDetermine
.
这篇关于为什么Objective-C NS_ENUM变量自动具有默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文