2013-06-18 110 views
1

我是新的使用核心數據。 我有類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' 
+2

擺脫'init'方法。 [這個答案](http://stackoverflow.com/questions/10489578/custom-initializer-for-an-nsmanagedobject)也會幫到你 – Alladinian

+0

@Alladinian:你說得對,那也可能導致問題。 –

回答

1

這可能意味着chatID不是財產的實際名稱作爲 核心數據模型檢查確定。

我總是建議讓Xcode(或像mogenerator這樣的工具)生成託管對象子類文件 而不是「手動」寫入它們。

+0

就是這樣!我的模型具有該類的不同屬性名稱。我自動再次生成了這個類!謝謝! – CainaSouza

+0

@CainaSouza:不客氣! –

1

無法識別的選擇器意味着應用程序試圖告訴對象執行它沒有的功能(例如,嘗試修改標籤的圖像,但它沒有圖像屬性,因此會引發無法識別的選擇器) 。在錯誤消息中,它試圖使用setChatID,它不存在。我在這臺計算機上沒有我的xcode項目,所以我無法檢查我的核心數據示例,但我會建議重新創建聊天對象並使用GUI創建您需要的屬性,然後自動創建該類。讓我知道你是否希望我幫助更多,今天晚些時候我會再次檢查當我有另一臺電腦時。