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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:07:20  来源:igfitidea点击:

convert NSNumber to BOOL objective C

iphoneobjective-ciosipad

提问by Eman87

I want to convert integers 0and 1to BOOLEAN YESand NO

我想转换整数01以布尔 YESNO

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 NSNumberis 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);

BOOLs are just signed chars, so you cannot use the %@format specifier, but you can use any of the integral format specifiers such as %d, %ior %c.

BOOLs 只是signed chars,因此您不能使用%@格式说明符,但您可以使用任何完整格式说明符,例如%d,%i%c

However, to output YESor NO, you'd need to use a string:

但是,要输出YESor NO,您需要使用字符串:

NSLog(@"bool is: %@", (myBool) ? @"YES" : @"NO");

回答by CRD

Given the incorrect assignment to NSNumberdid 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 intwith the value 0or 1to BOOLyou just cast:

如果是后者,然后转换成一个int以值01BOOL你只投:

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 BOOLvalue 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 Intto BOOL

隐藏IntBOOL

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, boolValuecan convert NSStringand NSNumber-> BOOL. boolValueworks :

其次,boolValue可以转换NSStringNSNumber-> BOOLboolValue作品:

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 intyou 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”