简体   繁体   English

Nsinteger可访问的问题

[英]Nsinteger accessible issue

When I try to add nsinteger value into array it shows warning, 当我尝试将nsinteger值添加到数组时,它显示警告,

Incompatible pointer to integer conversion sending 'NSInteger *' (aka 'int *') to parameter of type 'NSInteger' (aka 'int'); 不兼容的整数转换指针,将“ NSInteger *”(又名“ int *”)发送到类型为“ NSInteger”(又名“ int”)的参数; dereference with * 用*取消引用

and crashed when reach the code 并在到达代码时崩溃

[sizary1 addObject:[NSNumber numberWithInteger:Quant.quantity]];

quantity declared as 声明为的数量

 @property (nonatomic) NSInteger * quantity;

What change should I made? 我应该做些什么改变?

No need for * in NSInteger .Use 在NSInteger中不需要*。

@property (nonatomic) NSInteger  quantity;

Crash 崩溃

[NSNumber numberWithInteger:Quant.quantity]];

numberWithInteger: expect a value not its pointer reference so it crashes. numberWithInteger:期望一个值而不是它的指针引用,所以它会崩溃。

Make the property without * and it will work fine 使该属性不带* ,它将正常工作

1)You are using quantity as a Pointer in this case.So NSInteger doesn't allow to pointer in this case. 1)在这种情况下,您使用quantity作为指针,因此NSInteger在这种情况下不允许使用指针。

2)You're passing quantity to numberWithInteger:, which takes an NSInteger. 2)您正在将quantity传递给numberWithInteger :,这需要一个NSInteger。 It's nothing to do with the setObject:. 与setObject:无关。 You probably want to either copy quantity or just pass in quantity to setObject: directly. 您可能想要复制quantity或者只是直接将quantity传递给setObject:。

You are declaring a pointer to the NSInteger . 您正在声明一个指向NSInteger的指针。 NSNumber requires the NSInteger itself, not a pointer to it. NSNumber需要NSInteger本身,而不是指向它的指针。 I would change your property to 我会把你的财产改为

@property (nonatomic) NSInteger quantity;

NSInteger is a primitive type, which means it can be stored locally on the stack. NSInteger是原始类型,这意味着它可以本地存储在堆栈中。 You don't need to use a pointer to access it. 您无需使用指针即可访问它。 NSInteger is a primitive value type; NSInteger是原始值类型; you don't really need to use pointers. 您实际上不需要使用指针。 So your declaration should be 所以你的声明应该是

    @property (nonatomic) NSInteger quantity;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM