【问题标题】:Incompatible Integer to pointer conversion sending 'NSInteger' (aka 'int') to parameter of type 'NSInteger *' (aka 'int *')不兼容的整数到指针的转换将“NSInteger”(又名“int”)发送到“NSInteger *”(又名“int *”)类型的参数
【发布时间】:2014-04-10 23:53:54
【问题描述】:

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

【问题讨论】:

  • 观察到一种数据类型包含*,而另一种则没有。
  • @JamEngulfer221 发布setData_id:的定义。
  • 删除*。你不需要指针。
  • 这是一种很麻烦的数据转换方式。无需调用stringWithFormat——只需将integerValue直接应用于valueForKeyPath的结果即可。
  • Java 也有指针,它们只是称它们为“引用”,而你无法选择是否使用它们。

标签: ios objective-c nsdictionary


【解决方案1】:

您的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;

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

【讨论】:

  • 不,我希望最终结果是一个整数。 setData_id 将整数作为参数。我这里做的是将valueForKeyPath的结果解析成一个String,然后得到整数值。
  • 顺便说一下,setData_id是从一个属性合成的。
  • @JamEngulfer221 你定义的正确不正确。
  • 杀手。谢谢麦克马德
【解决方案2】:

属性定义不正确。

应该是:

@property (readwrite) NSInteger data_id;

而不是

@property (readwrite) NSInteger *data_id;

您正在尝试将整数值传递给期望具有指针类型的格式。

任意使用

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

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

如果您需要设置一个整数,请删除 [NSString stringWithFormat:@"%@"] - 这会创建一个字符串。

[activeItem setData_id:[[dict valueForKeyPath:@"data_id"] integerValue]];

【讨论】:

  • 不是“对象类型”——指针类型。问题在于 setData_id 的虚假定义。
  • 抱歉,还是错了。你还没有解决问题。
  • @HotLicks 嗯?在这里,我用粗体表示。 ;-)
  • 只有用所有相关信息更新问题后才能正确处理
【解决方案3】:

在适当的地方使用 integerValueintValue

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 2012-05-23
    • 1970-01-01
    • 2021-09-10
    相关资源
    最近更新 更多