我应该如何获取一个可以在 Objective-C 中分配 true、false 和 nil 的 bool 值? Objective-C 这样做的方式是什么?很像 C# 的 Nullable。
我希望能够使用 nil 值来表示未定义。
An NSNumber
实例可能会有所帮助。例如:
NSNumber *yesNoOrNil;
yesNoOrNil = [NSNumber numberWithBool:YES]; // set to YES
yesNoOrNil = [NSNumber numberWithBool:NO]; // set to NO
yesNoOrNil = nil; // not set to YES or NO
为了确定它的值:
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");
}
在 10.3 之后的所有 Mac OS X 平台和所有 iPhone OS 平台上,-[NSNumber boolValue] http://developer.apple.com/iphone/library/documentation/cocoa/reference/foundation/Classes/NSNumber_Class/Reference/Reference.html#//apple_ref/doc/uid/20000178-boolValue方法保证返回 YES 或 NO。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)