与指针转换不兼容的整数将'NSInteger'(又名'int')发送到'NSInteger*'类型的参数(又名'int*')

Jam*_*fer 8 objective-c nsdictionary ios

我正在尝试使用代码解析NSDictionary中的整数

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

但是,这给了我这个错误: Incompatible integer to pointer conversion sending 'NSInteger' (aka 'int') to parameter of type 'NSInteger *' (aka 'int *')

setData_id采用整数作为参数.如果我想解析一个字符串,[NSString stringWithFormat:@"%@", [dict valueForKeyPath:@"data_id"]]完美的工作.

我在这里做的是将valueForKeyPath的结果解析为String,然后从中解析一个整数.

Mac*_*ade 22

你的setData_id:方法是如何声明的?

看起来它期待一个NSInteger *而不是NSInteger......

它被声明为:

- ( void )setData_id: ( NSInteger )value;
Run Code Online (Sandbox Code Playgroud)

你可以使用你的代码.

否则,它意味着它被声明为:

- ( void )setData_id: ( NSInteger * )value;
Run Code Online (Sandbox Code Playgroud)

它可能是一个错字...如果你真的需要一个整数指针,那么你可以使用(假设你知道你在范围方面做了什么):

NSInteger i = [ [ NSString stringWithFormat: @"%@", [ dict valueForKeyPath: @"data_id" ] ] integerValue ];
[ activeItem setData_id: &i ];
Run Code Online (Sandbox Code Playgroud)

但我认为你只是犯了一个错字,添加一个指针(NSInteger *),而你的意思NSInteger.

注意:如果setData_id是属性,则同样适用:

@property( readwrite, assign ) NSInteger data_id;
Run Code Online (Sandbox Code Playgroud)

与:

@property( readwrite, assign ) NSInteger * data_id;
Run Code Online (Sandbox Code Playgroud)

我猜你写了第二个例子,虽然意思是第一个例子......