Objective-c 中枚举类型的 ivars 默认值
我有一个像这样的enum
:
typedef enum {
ThingA,
ThingB,
ThingC
} MyType;
我有一个类,在接口中声明了一个ivar,如下所示:
@property (nonatomic) MyType myTypeIvar;
当前myTypeIvar的默认值是ThingA(因为ThingA位于枚举中的第0个位置)。 设置 myTypeIvar ThingB 默认值的最佳方法是什么?
一种解决方案是简单地重新排序枚举定义(将 ThingB 放在第 0 个位置)。另一个解决方案是在 init
方法中设置 myTypeIvar。然而 init 方法不存在(并且当添加它时它永远不会被调用)。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在指定的初始值设定项(即所有其他初始值设定项都应调用的初始值设定项)中将
myTypeIvar
的值设置为ThingB
。例如,如果相关类是 UIViewController 的子类,您将重写-initWithNibName:bundle:
因为这是 UIViewController 的指定初始值设定项。Set the value of
myTypeIvar
toThingB
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.