Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incompatible Integer to pointer conversion sending 'NSInteger' (aka 'int') to parameter of type 'NSInteger *' (aka 'int *')

I'm trying to parse an integer from a NSDictionary using the code

[activeItem setData_id:[[NSString stringWithFormat:@"%@", [dict valueForKeyPath:@"data_id"]] integerValue]];

However, this is giving me this error: Incompatible integer to pointer conversion sending 'NSInteger' (aka 'int') to parameter of type 'NSInteger *' (aka 'int *')

setData_id takes an integer as a parameter. If I want to parse to a string, [NSString stringWithFormat:@"%@", [dict valueForKeyPath:@"data_id"]] works perfectly.

What I'm doing here is parsing the result of valueForKeyPath to a String, then parsing an integer from that.

like image 448
JamEngulfer Avatar asked Apr 10 '14 23:04

JamEngulfer


2 Answers

How is your setData_id: method declared?

Looks like it expect an NSInteger * rather than a NSInteger...

Either it is declared as:

- ( void )setData_id: ( NSInteger )value;

And you can use your code.

Otherwise, it means it is declared as:

- ( void )setData_id: ( NSInteger * )value;

It might be a typo... If you really need an integer pointer, then you may use (assuming you know what you are doing in terms of scope):

NSInteger i = [ [ NSString stringWithFormat: @"%@", [ dict valueForKeyPath: @"data_id" ] ] integerValue ];
[ activeItem setData_id: &i ];

But I think you just made a typo, adding a pointer (NSInteger *), while you meant NSInteger.

Note: If setData_id is a property, the same applies:

@property( readwrite, assign ) NSInteger data_id;

versus:

@property( readwrite, assign ) NSInteger * data_id;

I guess you wrote the second example, while meaning the first one...

like image 136
Macmade Avatar answered Oct 24 '22 13:10

Macmade


The property is defined incorrectly.

It should be:

@property (readwrite) NSInteger data_id;

instead of

@property (readwrite) NSInteger *data_id;

You are attempting to pass an integer value to a format that expects to have a pointer type.

Either use

[activeItem setData_id:[NSString stringWithFormat:@"%@", [dict valueForKeyPath:@"data_id"]]];

or

[activeItem setData_id:[NSString stringWithFormat:@"%d", [[dict valueForKeyPath:@"data_id"] integerValue]]];

If you need to set an integer, drop the [NSString stringWithFormat:@"%@"] - this creates a string.

[activeItem setData_id:[[dict valueForKeyPath:@"data_id"] integerValue]];
like image 21
Léo Natan Avatar answered Oct 24 '22 13:10

Léo Natan