I get an error “Invalid receiver type ‘NSInteger'” on this line in my implementation of a simple class:
self.price = p; // this line throws error
Should I be specifying price as copy? More details:
header file:
@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:
@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;
}
No, the default
assignis what you want.Frankly, this error doesn’t make sense to me — could there be something elsewhere in the code, such as an explicit implementation of
setPrice? In the meantime, grasping at straws, try omitting the accesses viaselfin this initialiser.(In all four of those assignments, actually. Your use of
copyis consistent with direct access to the ivars. If you are using acopysetter, you don’t need tocopythe argument preemptively, and doing it as you do here — with no correspondingrelease— will give you leaks. Stick to one way or the other.)