简体   繁体   中英

how to toggle a BOOL value in objective-c

I have the following:

for(CTVMenuItem *mi in selected){
  // if showFlag is YES -> NO; if NO -> YES
  mi.showFlag=@(!mi.showFlag);
}

but it is not working. Because of common items, I am having a hard time googling it. How would I make a YES become no and a NO become YES?

If showFlag is of type BOOL then it's simply:

mi.showFlag = !mi.showFlag;

If showFlag is actually an NSNumber representing a BOOL , then you want:

mi.showFlag = @(![mi.showFlag boolValue]);

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