简体   繁体   中英

“Incompatible pointer to integer conversion initializing 'int' with an expression of type 'NSNumber *'”

I suspect this is something really simple, but I'm trying to get an int that I have stored in a dictionary and the line I'm using is this..

 int mapX = [NSNumber numberWithInt:[templateObject valueForKey:@"mapX"]];

But it's giving me the error...

Incompatible pointer to integer conversion sending 'id' to parameter of type 'int'

and

Incompatible pointer to integer conversion initializing 'int' with an expression of type 'NSNumber *';

If I try...

NSNumber *mapX = [NSNumber numberWithInt:[templateObject valueForKey:@"mapX"]];

I basically get the same error. I know I'm probably just using the wrong syntax, but I'm not sure how else to write it.

Thanks for any help.

A couple of points:

  • [NSNumber numberWithInt:[templateObject valueForKey:@"mapX"]];

    Returns an NSNumber* , which you're trying to store in an int.

  • NSNumber* mapX = [NSNumber numberWithInt:[templateObject valueForKey:@"mapX"]];

    won't work either, because [templateObject valueForKey:@"mapX"] isn't an int . You can only store objects in NSDictionary , not primitive types.

You might want to try (Assuming I have the types correct)

NSNumber* mapXNum = [templateObject valueForKey:@"mapX"];
int mapX = [mapXNum intValue];
 int mapX = (int)[[templateObject valueForKey:@"mapX"] integerValue];

the above solution is assuming the templateObject valueForKey:@"mapX" returns an NSNumber. If it doesn't and you want to convert it to an NSNumber, use this solution instead:

 int mapX = (int)[(NSNumber *)[templateObject valueForKey:@"mapX"] integerValue];

Typecasting in ObjC is the.worst.

int mapX = [NSNumber numberWithInt:[templateObject valueForKey:@"mapX"]];

makes no sense for two reasons (think about it for a moment), however

NSNumber *mapX = [NSNumber numberWithInt:[[templateObject valueForKey:@"mapX"] intValue]];

should work assuming [templateObject valueForKey:@"mapX"] is either an NSNumber or an NSString instance.

You have two problems. First, [templateObject valueForKey:@"mapX"] returns an id , which is a pointer to an Objective-C object, not an int . And since the -[NSNumber numberWithInt:] method takes an int parameter, trying to convert an id to an int is an error.

The second problem is that you try to convert the result of [NSNumber numberWithInt:...] to an int , which is again the same problem: -[NSNumber numberWithInt:] returns a pointer to an NSNumber object, which is not an int .

Assuming that the value stored in your dictionary is in fact an NSNumber , what you really want to do is this:

NSNumber *mapXObj = [templateObject valueForKey:@"mapX"];  // Implicit cast from id to NSNumber*
int mapX = [mapXObj intValue];

Or more succinctly:

int mapX = [[templateObject valueForKey:@"mapX"] intValue];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM