Convert NSNumber to int in Objective-C
Asked Answered
H

5

132

I use [NSNumber numberWithInt:42] or @(42) to convert an int to NSNumber before adding it to an NSDictionary:

int intValue = 42;
NSNumber *numberValue = [NSNumber numberWithInt:intValue];
NSDictionary *dict = @{ @"integer" : numberValue };

When I retrieve the value from the NSDictionary, how can I transform it from NSNumber back to int?

NSNumber *number = dict[@"integer"];
int *intNumber = // ...?

It throws an exception saying casting is required when I do it this way:

int number = (int)dict[@"integer"];
Hammertoe answered 24/8, 2010 at 11:25 Comment(0)
N
207

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

NSNumber *number = [dict objectForKey:@"integer"];
int intValue = [number intValue];
Nazi answered 24/8, 2010 at 11:28 Comment(2)
Implicit conversion is now disallowed with ARC.Physics
basically [key intValue] (key is NSNumber)Descartes
R
62

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];
Raft answered 24/8, 2010 at 11:29 Comment(11)
Why should he stick to the NSInteger?Fitzhugh
NSInteger is a primitive type thats much faster during execution that NSNumber which is an object.Raft
And what is int? A primitive too?Fitzhugh
int is primitive too, yes. However it's maximum allowed number is less precisely defined. NSInteger is essentially just an alias to some primitive integer type that's known to be 32bit on 32bit machines and 64bit on 64bit devices.Raft
So it's a good idea to use NSInteger over int in the most cases?Fitzhugh
Yes, Apple suggests to.Raft
Seelmann, but I guess that if I know the numbers I need are not large, it's ok to use int over NSInteger, right?Fitzhugh
I'd recommend not guesstimating anything about native number arithmetics. You can sure use int, but there is little point in doing so, except for interfacing with cross-platform libs. And even then something like uint32_t would be better to use.Raft
I'm sorry if I'm bothering you, I'm just trying to learn and understand more. I recently started learning Objective C working on a big project and I'm thinking about ways to refactor it. So, it's a good idea to replace int with NSInteger everywhere possible in the existing project, right?Fitzhugh
Doing that would certainly make it more consistent with platform coding standards, yes.Raft
Always use NSInteger.Striker
A
3

Use the NSNumber method intValue

Here is Apple reference documentation

Accidental answered 24/8, 2010 at 11:27 Comment(0)
P
1

A less verbose approach:

int number = [dict[@"integer"] intValue];
Panocha answered 15/7, 2015 at 8:27 Comment(0)
R
0

A tested one-liner:

int number = ((NSNumber*)[dict objectForKey:@"integer"]).intValue;
Riba answered 27/9, 2019 at 15:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.