I am trying to declare a designated initializer like this:
- (instancetype)initWithDelegate:(id <MyDelegate>)delegate NS_DESIGNATED_INITIALIZER;
But it is showing me this compilation error:
Expected ':'
Interestingly when I try to write it like this (reference link: Adopting Modern Objective-C) -
- (instancetype)init NS_DESIGNATED_INITIALIZER;
It shows this error:
Expected ';' after method prototype.
Any ideas on how to properly use NS_DESIGNATED_INITIALIZER?
NS_DESIGNATED_INITIALIZER
macro is not defined in the library headers for Xcode 5 - you need Xcode 6 to use it. Note your link says "Pre-release".
The macro is defined in the following way (quoting 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
Note you can still use
- (instancetype)initWithDelegate:(id <MyDelegate>)delegate __attribute__((objc_designated_initializer));
in Xcode 5 or you can add that macro explicitly to your precompiled header.
Bear in mind you can only add this to interface or class extension declarations otherwise you'll get this error:
'objc_designated_initializer' attribute only applies to init methods of interface or class extension declarations
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With