ios 如何在objectiveC中将NSUInteger值转换为int值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16481884/
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
How do I convert NSUInteger value to int value in objectiveC?
提问by GR.
How do I convert NSUInteger
value to int
value in objectiveC?
如何在objectiveC中将值转换NSUInteger
为int
值?
and also how to print NSUInteger
value in NSLog
, I tried %@,%d,%lu
these are not working and throwing Ex-Bad Accesserror.
以及如何在 中打印NSUInteger
值NSLog
,我试过%@,%d,%lu
这些都不起作用并抛出Ex-Bad Access错误。
Thank you
谢谢
回答by Girish
回答by Steve Waddicor
Explicit cast to int
显式转换为 int
NSUInteger foo = 23;
int bar = (int)foo;
Although
虽然
NSLog(@"%lu",foo);
will work OK on current builds of OSX. It's not safe, as NSUInteger is typedeffed as unsigned int on other builds. Such as iOS. The strict answer is to cast it first:
将在当前版本的 OSX 上正常工作。这是不安全的,因为 NSUInteger 在其他版本中被定义为 unsigned int。比如iOS。严格的回答是先投:
NSLog(@"%lu",(unsigned long)foo);
回答by GR.
Hi all its working for me ...
嗨,它对我有用...
I tried this..
我试过这个..
NSUInteger inbox_count=9;
NSLog(@"inbox_count=%d",(int)inbox_count);
//output
inbox_count=9
thank to all..
谢谢大家。。
回答by Thilina Chamin Hewagama
It's simple as this,
就这么简单,
NSUInteger uintV = 890;
int intVal = (int) uintV;
NSLog(@"the value is : %lu", (unsigned long)intVal);
this will print something like this,
the value is : 890
这将打印这样的东西,
值是:890