获得可空布尔值的Objective-C方法是什么? [英] What is the Objective-C way of getting a nullable bool?
本文介绍了获得可空布尔值的Objective-C方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我应该如何获取可以在Objective-C中分配true,false和nil的布尔值?这样做的Objective-C方法是什么?就像C#的Nullable一样.
我希望能够使用nil值表示未定义.
解决方案
一个 NSNumber
实例可能会有所帮助.例如:
NSNumber * yesNoOrNil;yesNoOrNil = [NSNumber numberWithBool:YES];//设为YESyesNoOrNil = [NSNumber numberWithBool:NO];//设置为NOyesNoOrNil = nil;//未设置为YES或NO
为了确定其值:
if(yesNoOrNil == nil){NSLog(@值丢失!");}否则,如果([yesNoOrNil boolValue] == YES){NSLog(@值是YES");}否则,如果([yesNoOrNil boolValue] == NO){NSLog(@"Value is NO");}
在10.3之后的所有Mac OS X平台和所有iPhone OS平台上, 解决方案
An NSNumber
instance might help. For example:
NSNumber *yesNoOrNil;
yesNoOrNil = [NSNumber numberWithBool:YES]; // set to YES
yesNoOrNil = [NSNumber numberWithBool:NO]; // set to NO
yesNoOrNil = nil; // not set to YES or NO
In 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方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文