我是新的使用核心數據。 我有類Chat(NSManagedObject的子類),我需要用它創建新的對象。無法設置屬性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'
擺脫'init'方法。 [這個答案](http://stackoverflow.com/questions/10489578/custom-initializer-for-an-nsmanagedobject)也會幫到你 – Alladinian
@Alladinian:你說得對,那也可能導致問題。 –