Saran
Saran

Reputation: 6394

In Objective c how to define custom boolean type?

typedef signed char BOOL; 
// BOOL is explicitly signed so @encode(BOOL) == "c" rather than "C" 
// even if -funsigned-char is used.

#if __has_feature(objc_bool)
#define YES __objc_yes
#define NO  __objc_no
#else
#define YES ((BOOL)1)
#define NO  ((BOOL)0)
#endif

Above is how BOOL is defined in iOS. Following the same way i am trying to define another boolean with value ON OFF and did like below.

typedef signed char ONOFF;

#if __has_feature(objc_bool)
#define ON __objc_yes
#define OFF  __objc_no
#else
#define ON ((ONOFF)1)
#define OFF  ((ONOFF)0)
#endif

When this type defined as parameter autocompletion write it as 'int' instead of 'ONOFF'. But for BOOL type it rightly writing it as 'BOOL'.

Is that possible to create my custom boolean type that works similarly like BOOL in all aspects?

For some properties the readability will be better with ON/OFF, hence trying the above.

Any suggestions?

Edit

One quick work around to use ON/OFF in place of YES/NO is

typedef YES ON;
typedef NO OFF;

But still wondering why i cannot create my own boolean type.

Upvotes: 1

Views: 502

Answers (1)

user694733
user694733

Reputation: 16047

Keep it simple?

typedef BOOL ONOFF;
#define ON YES
#define OFF NO

Upvotes: 2

Related Questions