gpt4 book ai didi

iphone - 设置 NSInteger 属性时接收器类型无效 'NSInteger'

转载 作者:行者123 更新时间:2023-12-03 21:11:19 25 4
gpt4 key购买 nike

在简单类的实现中,我在这一行收到错误“无效接收者类型'NSInteger'”:

        self.price = p; // this line throws error

我应该将价格指定为copy吗?更多详情:

头文件:

@interface SafeItem : NSObject {
NSString *name;
NSInteger price;
NSString *category;
NSInteger itemid;
BOOL hasImage;
}

@property (nonatomic,copy) NSString *name;
@property (nonatomic) NSInteger price;
@property (nonatomic,copy) NSString *category;
@property NSInteger itemid;
@property BOOL hasImage;

- (id)initWithName:(NSString*) n price:(NSInteger) p category:(NSString*) c;

@end

实现:

@implementation SafeItem

@synthesize name, price, category, itemid, hasImage;

- (id)initWithName:(NSString*) n price:(NSInteger) p category:(NSString*) c {
if(self=[super init]){
self.itemid = [SafeItem getNextId];
self.name = [n copy];
self.price = p; // this line throws error
self.category = [c copy];
}
return self;
}

最佳答案

不,默认的分配就是您想要的。

坦率地说,这个错误对我来说没有意义——代码中是否有其他地方,例如 setPrice 的显式实现?与此同时,捕获救命稻草,尝试在此初始化程序中省略通过 self 进行的访问。

(实际上,在所有四个分配中。您对 copy 的使用与直接访问 ivars 是一致的。如果您使用 copy setter,则不需要不需要先发制人地复制参数,并且按照这里的方式执行 - 没有相应的release - 会给你泄漏。坚持一种方式或另一种方式.)

关于iphone - 设置 NSInteger 属性时接收器类型无效 'NSInteger',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3111700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com