2014-01-29 109 views
0

我想要一個字段添加到一個實體,每當我嘗試訪問新的領域,我得到與實體新的領域(無法識別的選擇錯誤)

無法識別的選擇發送到實例遇到問題.. 。

實體的其他一切工作。我能夠添加對象,我可以將值分配給對象中的其他字段,但不能分配給新對象。

值得注意的是,我還刪除了DerivedData目錄,並刪除了.mom/.momd文件以確保正確構建表。

有什麼想法?

Locations.h

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

@class Games, Players; 

@interface Locations : NSManagedObject 

@property (nonatomic, retain) NSNumber * defaultLoc; 
@property (nonatomic, retain) NSString * locationAddr1; 
@property (nonatomic, retain) NSString * locationAddr2; 
@property (nonatomic, retain) NSString * locationCity; 
@property (nonatomic, retain) NSNumber * locationID; 
@property (nonatomic, retain) NSString * locationName; 
@property (nonatomic, retain) NSString * locationState; 
@property (nonatomic, retain) NSString * locationZip; 
@property (nonatomic, retain) NSNumber * numberOfCourts; 
@property (nonatomic, retain) NSNumber * defLoc; 
@property (nonatomic, retain) NSSet *haveGames; 
@property (nonatomic, retain) NSSet *havePlayers; 
@end 

@interface Locations (CoreDataGeneratedAccessors) 

- (void)addHaveGamesObject:(Games *)value; 
- (void)removeHaveGamesObject:(Games *)value; 
- (void)addHaveGames:(NSSet *)values; 
- (void)removeHaveGames:(NSSet *)values; 

- (void)addHavePlayersObject:(Players *)value; 
- (void)removeHavePlayersObject:(Players *)value; 
- (void)addHavePlayers:(NSSet *)values; 
- (void)removeHavePlayers:(NSSet *)values; 

@end 

Locations.m

#import "Locations.h" 
#import "Games.h" 
#import "Players.h" 


@implementation Locations 

@dynamic defaultLoc; 
@dynamic locationAddr1; 
@dynamic locationAddr2; 
@dynamic locationCity; 
@dynamic locationID; 
@dynamic locationName; 
@dynamic locationState; 
@dynamic locationZip; 
@dynamic numberOfCourts; 
@dynamic defLoc; 
@dynamic haveGames; 
@dynamic havePlayers; 

@end 

注:defLoc和defaultLoc是我加了兩個字段。這些是給我的問題。

執行代碼:以上

- (IBAction)updateLocation:(UIStoryboardSegue *)segue 
{ 
    // If it is not "Edit" it is an "Add" and we need to insert a newobject. 
    if ([segueType1 isEqualToString:@"Add"]) 
    { 
     NSManagedObjectContext *context = [[self fetchedResultsController] managedObjectContext]; 
     location = [NSEntityDescription insertNewObjectForEntityForName:@"Locations" inManagedObjectContext:context]; 
    } 
    location.locationName = lName; 
    location.locationAddr1 = lAddr1; 
    location.locationAddr2 = lAddr2; 
    location.locationCity = lCity; 
    location.locationState = lState; 
    location.locationZip = lZip; 
    location.numberOfCourts = lNumCourts; 
    location.defLoc = lNumCourts; 
// location.defaultLoc = [NSNumber numberWithInt:1]; 
// DLog(@"ldefaultLocation = %@",ldefaultLocation); 
    location.defaultLoc = ldefaultLocation; 
    segueType1 = @"Add"; // Always reset back to Add so that segues work right 
} 

location.defLoc是給出了錯誤......所有其他位置的語句正常工作的命令。

+0

您是否創建了新的Core Data模型版本?如果是的話,你是否將「當前版本」設置爲新版本? –

+0

這似乎是正確的答案,因爲我之前沒有這樣做過。我做了關於如何實施,做了這個過程,嘗試刪除的東西,作爲一些建議......仍然沒有運氣:-(相同的消息 –

回答

0

當你沒有在你的NSManagedObject子類中聲明新屬性時就會發生這種情況。只要在核心數據模型中添加一個新字段,請記住將正確的屬性添加到NSManagedObject類並在實現中聲明@dynamic。

+0

我從菜單中運行「創建NSManagedOBject ...」的研究和雙重檢查,以確保它創建了實體的.h中的屬性,我甚至跑了兩次,以確保一切構建正確...一切似乎正常(除了這個錯誤信息)....還檢查了.m。它確實有@dynamic –

+0

好吧,你可以發佈一些更多的代碼嗎?例如:NSManagedObject子項中的屬性集以及用於實例化新實體的語句 –

相關問題