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

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

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

但是,这给了我这个错误:不兼容的整数到指针转换将’NSInteger'(又名’int’)发送到’NSInteger *’类型的参数(又名’int *’)

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

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

解决方法

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

看起来它期望NSInteger *而不是NSInteger ……

它被声明为:

- ( void )setData_id: ( NSInteger )value;

你可以使用你的代码.

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

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

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

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

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

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

@property( readwrite,assign ) NSInteger data_id;

与:

@property( readwrite,assign ) NSInteger * data_id;

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

以上是来客网为你收集整理的ios – 与指针转换不兼容的整数将’NSInteger'(又名’int’)发送到’NSInteger *’类型的参数(又名’int *’)全部内容,希望文章能够帮你解决ios – 与指针转换不兼容的整数将’NSInteger'(又名’int’)发送到’NSInteger *’类型的参数(又名’int *’)所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。