我是 Core Data 的新手。 我有 Chat 类(NSManagedObject 的子类),我需要用它创建新对象。
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface Chat : NSManagedObject
@property (nonatomic, retain) NSString *chatID;
@property (nonatomic, retain) NSString *modified;
@property (nonatomic, retain) NSString *opponentID;
@property (nonatomic, retain) NSString *opponentFacebookID;
@property (nonatomic, retain) NSString *opponentName;
@property (nonatomic, retain) NSString *lastMessage;
@end
#import "Chat.h"
@implementation Chat
@dynamic chatID;
@dynamic modified;
@dynamic opponentID;
@dynamic opponentFacebookID;
@dynamic opponentName;
@dynamic lastMessage;
- (id)init {
if (self = [super init])
{
}
return self;
}
@end
我像这样插入对象:
Chat *chat = (Chat *)[NSEntityDescription insertNewObjectForEntityForName:@"Chat" inManagedObjectContext:[[DataManager sharedInstance] managedObjectContext]];
chat.chatID = [chatDict objectForKey:@"id"];
chat.modified = [chatDict objectForKey:@"modified"];
chat.lastMessage = [chatDict objectForKey:@"last_message"];
但是我的应用程序在设置 chat.chatId
时崩溃了。它向我显示了这个错误,但不知道如何修复它!
2013-06-18 12:01:25.625 MyApp[5477:907] -[Chat setChatID:]: unrecognized selector sent to instance 0x1d8661d0
2013-06-18 12:01:25.627 MyApp[5477:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Chat setChatID:]: unrecognized selector sent to instance 0x1d8661d0'
最佳答案
这可能意味着 chatID
不是属性中定义的实际名称
核心数据模型检查器。
我总是建议让 Xcode(或“mogenerator”等工具)生成托管对象子类文件 而不是“手动”编写它们。
关于objective-c - 无法将属性设置为 NSManagedObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17172384/