将 BOOL 值转换为 INT 的正确方法?
每次当我尝试将 BOOL 值转换为 int 时。 INT 值显示 -8 表示 True,0 表示 False。理想情况下,它应该返回 1 表示 true。
我已经尝试了所有可能的转换方式,例如
int val=Info.isattachementDownloadable;
int val=[[NSString stringWithFormat:@"%d", Info.isattachementDownloadable] intValue];
int val=(int)Info.isattachementDownloadable;
Info.isattamentDownloadable
return BOOL
在所有方面都显示 -8 为 TRUE。
有什么建议吗?
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.
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
In all ways its showing -8 for TRUE.
Any suggestion ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
也许这会有所帮助(我想我不知道为什么首先需要它)
希望它对您有帮助。
Maybe that will help(thought i don't know why it may be needed in the first place)
Hope that it helps you.
这样做要快得多:
如果
myBool
为 YES,myInt
将为 1,如果myBool
为 NO,则为 0。It is much quicker to do it like this:
myInt
will be 1 ifmyBool
is YES, and 0 ifmyBool
is NO.始终可以使用数字基元(例如 C 的 int)的面向对象包装器。那我是在回答问题吗?是或否取决于您的观点。但是,当使用 BOOL 作为输入时,我更喜欢使用以下内容:
如果您确实需要使用原始数据类型:
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:
And if you really need to use the primitive datatype:
您可以将 BOOL 值分配给 int,因为 YES 和 NO 只是 1 和 0 的快捷方式。您还可以对 BOOL 执行数学运算。
仅供参考,BOOL 甚至可以保存从 -128 到 127 的值,因为它们只是有符号字符的类型定义。
参考:https://www.bignerdranch.com/blog/bools-sharp-corners/
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.
Reference: https://www.bignerdranch.com/blog/bools-sharp-corners/