我正在开发我的第一个 Core Data 项目,它在 iOS 模拟器中编译得很好,但是当我按下“+”按钮时,我收到了错误消息:
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
BNR项目.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