How should I go about getting a bool value that I can assign true, false and nil to in Objective-C? What is the Objective-C way of doing this? Much like C#'s Nullable.I'm looking to be able to use the nil value to represent undefined. 解决方案 An NSNumber instance might help. For example:NSNumber *yesNoOrNil;yesNoOrNil = [NSNumber numberWithBool:YES]; // set to YESyesNoOrNil = [NSNumber numberWithBool:NO]; // set to NOyesNoOrNil = nil; // not set to YES or NOIn order to determine its value:if (yesNoOrNil == nil){ NSLog (@"Value is missing!");}else if ([yesNoOrNil boolValue] == YES){ NSLog (@"Value is YES");}else if ([yesNoOrNil boolValue] == NO){ NSLog (@"Value is NO");}On all Mac OS X platforms after 10.3 and all iPhone OS platforms, the -[NSNumber boolValue] method is guaranteed to return YES or NO. 这篇关于获得可空布尔值的Objective-C方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-06 05:06