Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert NSNumber to int in Objective-C

Have a look at the documentation. Use the intValue method:

NSNumber *number = [dict objectForKey:@"integer"];
int intValue = [number intValue];

You should stick to the NSInteger data types when possible. So you'd create the number like that:

NSInteger myValue = 1;
NSNumber *number = [NSNumber numberWithInteger: myValue];

Decoding works with the integerValue method then:

NSInteger value = [number integerValue];

Use the NSNumber method intValue

Here is Apple reference documentation


A tested one-liner:

int number = ((NSNumber*)[dict objectForKey:@"integer"]).intValue;

A less verbose approach:

int number = [dict[@"integer"] intValue];