Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert NSUInteger value to int value in objectiveC? [duplicate]

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

and also how to print NSUInteger value in NSLog , I tried %@,%d,%lu these are not working and throwing Ex-Bad Access error.

Thank you

like image 802
GR. Avatar asked May 10 '13 12:05

GR.


2 Answers

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

for more reference, look here

like image 181
Girish Avatar answered Oct 04 '22 08:10

Girish


Explicit cast to 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:

NSLog(@"%lu",(unsigned long)foo); 
like image 40
Steve Waddicor Avatar answered Oct 04 '22 06:10

Steve Waddicor