2013-03-29 28 views
1

我正在使用CoreData與該實體的一個實體和兩個屬性。
實體:粘合劑
屬性:名稱,lastOpened
,BR> 我能夠插入實體的一個新的對象沒有問題,而且我還可以設置它的名字,但我不能設置它的lastOpened屬性。
這裏是我的代碼:
CoreData - insertNewObjectForEntityForName不能設置屬性

Binder *newBinder = [NSEntityDescription insertNewObjectForEntityForName:@"Binder" inManagedObjectContext:context]; 
[newBinder setName:@"Binder"]; 
[newBinder setLastOpened:[NSDate date]]; //Tried this first 
newBinder.lastOpened = [NSDate date]; //No compiler warning either 

然而,當我運行應用程序,我得到的-[Binder setLastOpened:]: unrecognized selector sent to instance 0x9688870

一個錯誤,我可以確認所顯示的內存地址實際上是正確的Binder對象。任何想法,爲什麼我可以設置一個屬性,但不是另一個?謝謝。
Binder.h:

#import <Foundation/Foundation.h> 
#import <CoreData/CoreData.h> 

@class Cards; 

@interface Binder : NSManagedObject 

@property (nonatomic, retain) NSString * name; 
@property (nonatomic, retain) NSDate * lastOpened; 
@property (nonatomic, retain) NSSet *cards; 
@end 

@interface Binder (CoreDataGeneratedAccessors) 

- (void)addCardsObject:(Cards *)value; 
- (void)removeCardsObject:(Cards *)value; 
- (void)addCards:(NSSet *)values; 
- (void)removeCards:(NSSet *)values; 

@end 

Binder.m:

#import "Binder.h" 
#import "Cards.h" 


@implementation Binder 

@dynamic name; 
@dynamic lastOpened; 
@dynamic cards; 

@end 
+0

似乎你的模型沒有'lastOpened'這樣的屬性。打印您的管理對象模型並檢查。 –

回答

1

Xcode中偶爾糊里糊塗了重建的變化xcdatamodel。下一次,嘗試做一個乾淨的構建。

此外,你不應該核心類名字符串,因爲它會破壞重構。

[NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([Binder class]) inManagedObjectContext:context] 

另外,爲類名和管理對象添加一個前綴。 (例如,喜歡XYZBinder只是活頁夾)。您將避免未來命名空間碰撞的悲傷。

+0

感謝這個答案Lorean,它真的幫了我。但是,每次我重建應用程序時,我都需要執行Clean and Clean Build。任何想法如何我可以永久解決這個問題? –

0

您可能已經加載在您的項目命名爲粘結劑另一個類。嘗試將您的CoreData實體的類名設置爲XCode中的BinderMO,並重新創建類文件。

0

我不知道是什麼問題,但我刪除了我的Binder.h/.m文件並重新創建它們,現在它可以工作。舊的和新的唯一的區別是'名字'和'lastOpened'交換位置。謝謝您的幫助。

新Binder.h:

#import <Foundation/Foundation.h> 
#import <CoreData/CoreData.h> 

@class Cards; 

@interface Binder : NSManagedObject 

@property (nonatomic, retain) NSDate * lastOpened; 
@property (nonatomic, retain) NSString * name; 
@property (nonatomic, retain) NSSet *cards; 
@end 

@interface Binder (CoreDataGeneratedAccessors) 

- (void)addCardsObject:(Cards *)value; 
- (void)removeCardsObject:(Cards *)value; 
- (void)addCards:(NSSet *)values; 
- (void)removeCards:(NSSet *)values; 

@end 

新Binder.m:

#import "Binder.h" 
#import "Cards.h" 


@implementation Binder 

@dynamic lastOpened; 
@dynamic name; 
@dynamic cards; 

@end