Search code examples
iosobjective-cnsdictionary

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.


Solution

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