1
我正在處理我的第一個Core Data項目,並且它在iOS模擬器中編譯得很好,但是當我按下'+'按鈕時,出現錯誤消息:CoreData:error:無法調用NSManagedObject類'BNRItem'上的指定初始值設定項'
2012-05-05 10:23:05.195 Homepwner[15387:fb03] CoreData: error: Failed to call designated initializer on NSManagedObject class 'BNRItem'
2012-05-05 10:23:05.197 Homepwner[15387:fb03] -[BNRItem itemName]: unrecognized selector sent to instance 0x6d50790
2012-05-05 10:23:05.198 Homepwner[15387:fb03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[BNRItem itemName]: unrecognized selector sent to instance 0x6d50790'
*** First throw call stack:
(0x1082022 0x1533cd6 0x1083cbd 0xfe8ed0 0xfe8cb2 0x58fb 0x320c 0x1083e99 0x30314e 0x541a0e 0x1083e99 0x30314e 0x3030e6 0x3a9ade 0x3a9fa7 0x3a9266 0x3283c0 0x3285e6 0x30edc4 0x302634 0x1d3def5 0x1056195 0xfbaff2 0xfb98da 0xfb8d84 0xfb8c9b 0x1d3c7d8 0x1d3c88a 0x300626 0x271d 0x2685)
terminate called throwing an exception(lldb)
試圖檢查我的代碼在BNRItem.h,.m,它看起來沒問題。你能找到問題嗎?這裏是我的代碼:
BNRItem.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface BNRItem : NSManagedObject
@property (nonatomic, retain) NSString * itemName;
@property (nonatomic, retain) NSString * serialNumber;
@property (nonatomic) int32_t valueInDollars;
@property (nonatomic) NSTimeInterval dateCreated;
@property (nonatomic, retain) NSString * imageKey;
@property (nonatomic, retain) NSData * thumbnailData;
@property (nonatomic, strong) UIImage *thumbnail;
@property (nonatomic) double orderingValue;
@property (nonatomic, retain) NSManagedObject *assetType;
-(void)setThumbnailDataFromImage:(UIImage *)image;
@end
BNRItem.m
#import "BNRItem.h"
@implementation BNRItem
@dynamic itemName;
@dynamic valueInDollars;
@dynamic thumbnailData;
@dynamic imageKey;
@dynamic serialNumber;
@dynamic dateCreated;
@dynamic thumbnail;
@dynamic orderingValue;
@dynamic assetType;
-(void)awakeFromFetch
{
[super awakeFromFetch];
UIImage *tn = [UIImage imageWithData:[self thumbnailData]];
[self setPrimitiveValue:tn forKey:@"thumbnail"];
}
-(void)awakeFromInsert
{
[super awakeFromInsert];
NSTimeInterval t = [[NSDate date] timeIntervalSinceReferenceDate];
[self setDateCreated:t];
}
- (void)setThumbnailDataFromImage:(UIImage *)image
{
CGSize origImageSize = [image size];
CGRect newRect = CGRectMake(0, 0, 40, 40);
float ratio = MAX(newRect.size.width/origImageSize.width,
newRect.size.height/origImageSize.height);
UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0);
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:newRect
cornerRadius:5.0];
[path addClip];
CGRect projectRect;
projectRect.size.width = ratio * origImageSize.width;
projectRect.size.height = ratio * origImageSize.height;
projectRect.origin.x = (newRect.size.width - projectRect.size.width)/2.0;
projectRect.origin.y = (newRect.size.height - projectRect.size.height)/2.0;
[image drawInRect:projectRect];
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
[self setThumbnail:smallImage];
NSData *data = UIImagePNGRepresentation(smallImage);
[self setThumbnailData:data];
UIGraphicsEndImageContext();
}
@end
謝謝,我也遇到了錯誤。我重新閱讀本節,並給出了以下說明:「您可以構建以檢查語法錯誤。」但它不建議運行它。 – patrickandroid