我正在尝试声明这样的指定初始值设定项:
- (instancetype)initWithDelegate:(id <MyDelegate>)delegate NS_DESIGNATED_INITIALIZER;
但它向我显示了这个编译错误:
Expected ':'
有趣的是,当我尝试这样写时(参考链接:Adopting Modern Objective-C) -
- (instancetype)init NS_DESIGNATED_INITIALIZER;
显示此错误:
Expected ';' after method prototype.
有关如何正确使用NS_DESIGNATED_INITIALIZER的任何想法?
答案 0 :(得分:34)
NS_DESIGNATED_INITIALIZER
宏 - 您需要Xcode 6才能使用它。请注意您的链接显示&#34;预发布&#34;。
宏按以下方式定义(引用NSObjCRuntime.h
)
#ifndef NS_DESIGNATED_INITIALIZER
#if __has_attribute(objc_designated_initializer)
#define NS_DESIGNATED_INITIALIZER __attribute__((objc_designated_initializer))
#else
#define NS_DESIGNATED_INITIALIZER
#endif
#endif
请注意,您仍然可以使用
- (instancetype)initWithDelegate:(id <MyDelegate>)delegate __attribute__((objc_designated_initializer));
答案 1 :(得分:2)
请记住,您只能将此添加到接口或类扩展声明中,否则您将收到此错误:
'objc_designated_initializer' attribute only applies to init methods of interface or class extension declarations