Everytime when I tried to convert BOOL value into int. INT value is showing -8 for True and 0 for False. Ideally it should return 1 for true.
每当我尝试将BOOL值转换为int时。 INT值显示-8表示True,0表示False。理想情况下,它应返回1表示true。
I have tried all possible ways of conversion like
我已经尝试过所有可能的转换方式
int val=Info.isattachementDownloadable;
int val=[[NSString stringWithFormat:@"%d", Info.isattachementDownloadable] intValue];
int val=(int)Info.isattachementDownloadable;
where Info.isattachementDownloadable
return BOOL
其中Info.isattachementDownloadable返回BOOL
In all ways its showing -8 for TRUE.
在所有方面,它显示-8为真。
Any suggestion ?
有什么建议吗?
5 个解决方案
#1
26
Maybe that will help(thought i don't know why it may be needed in the first place)
也许这会有所帮助(以为我不知道为什么可能首先需要它)
NSInteger i = @(YES).integerValue;
Hope that it helps you.
希望它能帮到你。
#2
21
It is much quicker to do it like this:
这样做要快得多:
BOOL myBool = YES;
int myInt = (myBool ? 1 : 0);
myInt
will be 1 if myBool
is YES, and 0 if myBool
is NO.
如果myBool为YES,则myInt为1,如果myBool为NO,则为0。
#3
2
It is always possible to use the object-oriented wrapper around numeric primitives (e.g. C's int). So am I answering the question? both yes and no depending on your viewpoint. However, here is what I prefer to use when using BOOL as input:
始终可以在数字基元周围使用面向对象的包装器(例如C的int)。我在回答这个问题吗?是和否取决于您的观点。但是,这是我在使用BOOL作为输入时更喜欢使用的内容:
NSNumber *numval = [NSNumber numberWithBool:Info.isattachementDownloadable];
And if you really need to use the primitive datatype:
如果你真的需要使用原始数据类型:
int val = [numval intValue];
#4
1
BOOL myBool;
int myInt;
if (myBool) myInt = 1;
else myInt = 0;
#5
1
You can just assign BOOL value to int since YES and NO are just shortcuts for 1 and 0. You can also perform maths operations on BOOLs. FYI, BOOLs can even hold values from -128 up to 127 since they are just typedefs for signed chars.
您可以将BOOL值分配给int,因为YES和NO只是1和0的快捷方式。您还可以对BOOL执行数学运算。仅供参考,BOOL甚至可以保持-128到127之间的值,因为它们只是签名字符的typedef。
BOOL someBool = YES;
int intFromBool = someBool;
NSLog(@"%d", intFromBool);
intFromBool -= YES;
NSLog(@"%d", intFromBool);
Reference: https://www.bignerdranch.com/blog/bools-sharp-corners/
参考:https://www.bignerdranch.com/blog/bools-sharp-corners/
#1
26
Maybe that will help(thought i don't know why it may be needed in the first place)
也许这会有所帮助(以为我不知道为什么可能首先需要它)
NSInteger i = @(YES).integerValue;
Hope that it helps you.
希望它能帮到你。
#2
21
It is much quicker to do it like this:
这样做要快得多:
BOOL myBool = YES;
int myInt = (myBool ? 1 : 0);
myInt
will be 1 if myBool
is YES, and 0 if myBool
is NO.
如果myBool为YES,则myInt为1,如果myBool为NO,则为0。
#3
2
It is always possible to use the object-oriented wrapper around numeric primitives (e.g. C's int). So am I answering the question? both yes and no depending on your viewpoint. However, here is what I prefer to use when using BOOL as input:
始终可以在数字基元周围使用面向对象的包装器(例如C的int)。我在回答这个问题吗?是和否取决于您的观点。但是,这是我在使用BOOL作为输入时更喜欢使用的内容:
NSNumber *numval = [NSNumber numberWithBool:Info.isattachementDownloadable];
And if you really need to use the primitive datatype:
如果你真的需要使用原始数据类型:
int val = [numval intValue];
#4
1
BOOL myBool;
int myInt;
if (myBool) myInt = 1;
else myInt = 0;
#5
1
You can just assign BOOL value to int since YES and NO are just shortcuts for 1 and 0. You can also perform maths operations on BOOLs. FYI, BOOLs can even hold values from -128 up to 127 since they are just typedefs for signed chars.
您可以将BOOL值分配给int,因为YES和NO只是1和0的快捷方式。您还可以对BOOL执行数学运算。仅供参考,BOOL甚至可以保持-128到127之间的值,因为它们只是签名字符的typedef。
BOOL someBool = YES;
int intFromBool = someBool;
NSLog(@"%d", intFromBool);
intFromBool -= YES;
NSLog(@"%d", intFromBool);
Reference: https://www.bignerdranch.com/blog/bools-sharp-corners/
参考:https://www.bignerdranch.com/blog/bools-sharp-corners/