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.
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...
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]];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With