我得到這一行的錯誤「無效的接收器類型‘NSInteger的’」我在執行一個簡單的類:無效的接收器類型「NSInteger的」時設置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;
}
謝謝。我認爲代碼的另一部分出現了問題(雖然肯定沒有明確實現任何setter)。順便說一下,對於未來的讀者:不要*刪除'自我'和'複製'(例如「名稱= n」;做「self.name = n」或「name = [n copy]」,最好前者)。在嘗試訪問這些字符串時,我最終在嘗試各種建議時最終刪除了這兩個建議,並以EXC_BAD_ACCESS結尾。 – unsorted 2010-06-25 07:26:34