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;

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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...