简体   繁体   中英

Objective-C ternary operator resulting in @“null” or Boolean

I have this line:

watchAgain == nil ? @"null" : (watchAgain ? 1 : 0)

watchAgain could be nil , YES or NO .

What's my best bet for either getting @"null" , 1 or 0 depending on the value?

At the moment I receive the error: Incompatible operand types ('NSString *' and 'int')

I'm new to Objective-C, so I'm guessing this might be quite a simple fix!

EDIT (context):

I have a toggle state in my iOS UI, so either YES or NO. However, I'd like to send a 3rd state to my Node.js backend of "they didn't select on or off", hence why I was thinking of sending a "null" string...

For if you are setting button title

NSString *titleForButton = watchAgain == nil ? @"null" : (watchAgain ? @"YES" : @"NO");

As far as i think you are trying to achieve is setting button title according to watchAgain variable, so the above titleForButton could be the title you would like to set.

What you were doing wrong in

watchAgain == nil ? @"null" : (watchAgain ? 1 : 0)

is assigning either string value or an integer value to a variable, so you should make it clear for compiler whether you are assigning a string or integer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM