JamEngulfer
JamEngulfer

Reputation: 747

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.

Upvotes: 10

Views: 42742

Answers (4)

Pradeep Reddy Kypa
Pradeep Reddy Kypa

Reputation: 4022

Xcode 15.3 has started throwing errors with incompatible pointer types. Quick fix is to see if there are any instances where NSInteger variable is declared as

NSInteger *index;

and replace it with

NSInteger index

It should work with the above fix.

Upvotes: 0

zeeawan
zeeawan

Reputation: 6905

Use integerValue and intValue where appropriate.

Upvotes: 0

Macmade
Macmade

Reputation: 54039

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...

Upvotes: 27

Léo Natan
Léo Natan

Reputation: 57060

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]];

Upvotes: 4

Related Questions