CoreData:错误:无法在 NSManagedObject 类“BNRItem”上调用指定的初始化程序
Posted
技术标签:
【中文标题】CoreData:错误:无法在 NSManagedObject 类“BNRItem”上调用指定的初始化程序【英文标题】:CoreData: error: Failed to call designated initializer on NSManagedObject class 'BNRItem' 【发布时间】:2012-05-05 17:37:49 【问题描述】:我正在处理我的第一个 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
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
【问题讨论】:
【参考方案1】:我猜你正在关注大书呆子的书?我想他们应该提到应用程序将在这个阶段运行......
我在学习教程的过程中遇到了同样的问题。经过一番搜索,我发现这是因为你不应该像 BNRItem 那样分配/初始化 NSManagedObject。
实际上,以下部分将向您展示如何用新实现替换 createItem 方法以从数据库中检索 BNRItem 对象。
【讨论】:
谢谢,我也遇到了错误。我重新阅读了该部分,它给出了以下说明:“您可以构建以检查语法错误。”但它不建议运行它。以上是关于CoreData:错误:无法在 NSManagedObject 类“BNRItem”上调用指定的初始化程序的主要内容,如果未能解决你的问题,请参考以下文章
在 NSManagedObject 类中初始化 @NSManaged 变量?
在 Core Data 中,NSManaged 对象上的 URI 属性类型应该用于啥?
对于 @NSManaged 变量,fileprivate 是不是足够?