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

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

How do I convert NSUInteger value to int value in objectiveC?

iosobjective-cintnsuinteger

提问by GR.

How do I convert NSUIntegervalue to intvalue in objectiveC?

如何在objectiveC中将值转换NSUIntegerint值?

and also how to print NSUIntegervalue in NSLog, I tried %@,%d,%luthese are not working and throwing Ex-Bad Accesserror.

以及如何在 中打印NSUIntegerNSLog,我试过%@,%d,%lu这些都不起作用并抛出Ex-Bad Access错误。

Thank you

谢谢

回答by Girish

NSUInteger intVal = 10;
int iInt1 = (int)intVal;
NSLog(@"value : %lu %d", (unsigned long)intVal, iInt1);

for more reference, look here

如需更多参考,请看这里

回答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