代码之家  ›  专栏  ›  技术社区  ›  unsorted

设置NSInteger属性时接收器类型“NSInteger”无效

  •  2
  • unsorted  · 技术社区  · 14 年前

            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;
    }
    
    2 回复  |  直到 14 年前
        1
  •  1
  •   walkytalky    14 年前

    不,默认值 assign 是你想要的。

    setPrice ? 同时,抓住吸管,试着省去这些通道 self 在这个初始化器中。

    copy 与直接访问ivars一致。如果您使用的是 塞特,你不需要 复制 release --会让你泄密。坚持这样或那样。)

        2
  •  1
  •   Endemic    14 年前

    (copy) . copy retain 仅用于对象,而不是NSInteger之类的基本值。

    (副本) 作为名称和类别属性的属性,但在赋值时再次复制它们,这是多余的。指定 (副本) 对于一个属性,意味着每当您将该属性设置为一个新值时, 自动调用。