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

标签 ios objective-c nsdictionary

我正在尝试使用代码从 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 的结果解析为一个字符串,然后从中解析一个整数。

最佳答案

您的 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 *')的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23001146/

相关文章:

ios - 具有两种不同文本对齐方式的 NSAttributedString

iphone - 在 Objective-C 中是否有检测 AirPlay 的通知?

ios - UILabel LineBreakMethod

ios - 排序用户位置 iOS

cocoa - NSDictionary objectForKey/valueForKey 已格式化?

ios - 介绍 MFMessageComposeViewController/理解 DispatchQueue.main.async

ios - 在 iOS 9 的一个 View Controller 上隐藏状态栏

ios - trackWithMediaType 有时返回 0 首轨道

iphone - 如何检测 UIImageView 的宽度和高度

ios - 比较两个 NSDictionaries 并找出差异