Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NS_DESIGNATED_INITIALIZER expected : (colon)

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?

like image 722
Devarshi Avatar asked Jun 09 '14 12:06

Devarshi


2 Answers

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.

like image 139
Sulthan Avatar answered Nov 06 '22 02:11

Sulthan


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
like image 2
Snowcrash Avatar answered Nov 06 '22 01:11

Snowcrash