ios 将 NSNumber 转换为 BOOL 目标 C
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10341735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
convert NSNumber to BOOL objective C
提问by Eman87
I want to convert integers 0
and 1
to BOOLEAN YES
and NO
我想转换整数0
并1
以布尔 YES
和NO
My code:
我的代码:
NSNumber *num = 0;
BOOL myBool = [num boolValue];
NSLog(@"bool is: %@",myBool);
It gives output as (null)
它给出输出为 (null)
What could be wrong?
可能有什么问题?
回答by Jacob Relkin
First of all, the initialization of your NSNumber
is incorrect. You should use one of the +numberWith:
class methods defined on NSNumber
:
首先,你的初始化NSNumber
不正确。您应该使用+numberWith:
在 上定义的类方法之一NSNumber
:
+ (NSNumber *)numberWithChar:(char)value;
+ (NSNumber *)numberWithUnsignedChar:(unsigned char)value;
+ (NSNumber *)numberWithShort:(short)value;
+ (NSNumber *)numberWithUnsignedShort:(unsigned short)value;
+ (NSNumber *)numberWithInt:(int)value;
+ (NSNumber *)numberWithUnsignedInt:(unsigned int)value;
+ (NSNumber *)numberWithLong:(long)value;
+ (NSNumber *)numberWithUnsignedLong:(unsigned long)value;
+ (NSNumber *)numberWithLongLong:(long long)value;
+ (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value;
+ (NSNumber *)numberWithFloat:(float)value;
+ (NSNumber *)numberWithDouble:(double)value;
+ (NSNumber *)numberWithBool:(BOOL)value;
+ (NSNumber *)numberWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0);
+ (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0);
BOOL
s are just signed char
s, so you cannot use the %@
format specifier, but you can use any of the integral format specifiers such as %d
, %i
or %c
.
BOOL
s 只是signed char
s,因此您不能使用%@
格式说明符,但您可以使用任何完整格式说明符,例如%d
,%i
或%c
。
However, to output YES
or NO
, you'd need to use a string:
但是,要输出YES
or NO
,您需要使用字符串:
NSLog(@"bool is: %@", (myBool) ? @"YES" : @"NO");
回答by CRD
Given the incorrect assignment to NSNumber
did you really mean to create an object containing an integer, or is your title question "convert int to BOOL" correct?
鉴于分配不正确,NSNumber
您是否真的想创建一个包含整数的对象,或者您的标题问题“将 int 转换为 BOOL”是否正确?
If the latter then to convert an int
with the value 0
or 1
to BOOL
you just cast:
如果是后者,然后转换成一个int
以值0
或1
给BOOL
你只投:
int num = ...; // num is 0 or 1
BOOL myBool = (BOOL)num; // myBool is NO or YES
However a better conversion is:
然而,更好的转换是:
BOOL myBool = num != 0; // myBool is NO for 0, YES for anything else
as this follows the Obj-C (and C) notion that zero is false and non-zero true.
因为这遵循 Obj-C(和 C)的概念,即零为假,非零为真。
If you wish to convert a BOOL
value to the strings "YES"
and "NO"
then this simple function will do it efficiently:
如果您希望将BOOL
值转换为字符串"YES"
,"NO"
那么这个简单的函数将有效地完成:
NS_INLINE NSString *BoolToStr(BOOL b) { return b ? @"YES" : @"NO"; }
just place it in a convenient header and import wherever you need it.
只需将它放在一个方便的标题中,然后在您需要的任何地方导入。
回答by Nikhlesh Bagdiya
To Covert Int
to BOOL
隐藏Int
到BOOL
convert an int with the value 0 or 1 to BOOL you just cast
将值为 0 或 1 的 int 转换为您刚刚投射的 BOOL
NSNumber *num = [NSNumber numberWithInt:0]; // num is 0 or 1
BOOL myBool = [num boolValue]; // myBool is NO or YES
回答by deathhorse
As sch mentioned you messed it up right here:
正如 sch 提到的,你在这里搞砸了:
NSNumber *num = 0;
- asterisk is a pointer/reference to an object ( a block of memory in your RAM ). It can hold just the memory address value ( no integers/bools ) of memory block for your object. Even in Objective-C don't mistake BOOL, int, float, double, NSInteger numeric types with objects (NSNumber).
- 星号是指向对象(RAM 中的一块内存)的指针/引用。它可以只保存对象的内存块的内存地址值(没有整数/布尔值)。即使在 Objective-C 中,也不要将 BOOL、int、float、double、NSInteger 数字类型与对象 (NSNumber) 混淆。
回答by HelmiB
as point it out by @jacob Relkin. you are wrong in use of NSNumber
.
正如@jacob Relkin 指出的那样。你用错了NSNumber
。
secondly, boolValue
can convert NSString
and NSNumber
-> BOOL
. boolValue
works :
其次,boolValue
可以转换NSString
和NSNumber
-> BOOL
。boolValue
作品:
Skips initial space characters (whitespaceSet), or optional -/+ sign followed by zeroes. Returns YES on encountering one of "Y", "y", "T", "t", or a digit 1-9. It ignores any trailing characters.
跳过初始空格字符 (whitespaceSet),或可选的 -/+ 符号后跟零。遇到“Y”、“y”、“T”、“t”或数字1-9之一时返回 YES 。它忽略任何尾随字符。
for int
you could use it as boolean
因为int
你可以用它作为boolean
BOOL a = num; //will behave just like boolValue.
布尔a = num; // 将表现得像 boolValue。
回答by pedrotorres
i do not know if im completely right about this but when you assign a NSNumber as '0' you're giving the value 'nil' if you want the number you should go for '@0' or '@1' or use the method "numberwithint"
我不知道我对此是否完全正确,但是当您将 NSNumber 分配为“0”时,如果您想要该数字,您应该使用“@0”或“@1”或使用方法“numberwithint”