2017-06-21 80 views
0

我有一個名爲天氣在我的核心數據在以下形式的實體:嘗試訪問核心數據模型的NSNumber的屬性給EXC_BAD_ACCESS

xcdatamodel

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

@interface WeatherModel : NSManagedObject 

@property (nonatomic, retain) NSString * cityName; 
@property (nonatomic, retain) NSNumber * latitude; 
@property (nonatomic, retain) NSNumber * longitude; 
@property (nonatomic, retain) NSNumber * temperature; 
@property (nonatomic, retain) NSNumber * humidity; 
@property (nonatomic, retain) NSNumber * windSpeed; 
@property (nonatomic, retain) NSNumber * rain; 
@property (nonatomic, retain) NSDate * date; 
@property (nonatomic, retain) NSString * iconId; 
@property (nonatomic, retain) NSString * units; 

@end 

我保存這個模型的核心數據的情況下,如下:

WeatherModel *bookmarkWeatherModel = (WeatherModel*)[DatabaseHelper insertNewEntityWithName:@"Weather" andContext:self.weatherManagedObjectContext]; 

[bookmarkWeatherModel setLatitude:[NSNumber numberWithDouble:0.0]]; 
[bookmarkWeatherModel setLongitude:[NSNumber numberWithDouble:0.0]]; 
[bookmarkWeatherModel setCityName:@"CityA"]; 
NSLog(@"%@", bookmarkWeatherModel.cityName); 
NSLog(@"%@", bookmarkWeatherModel.latitude); 

[DatabaseHelper saveCoreData:self.weatherManagedObjectContext]; 

但是每當我試圖(甚至稱saveCoreData方法之後)訪問任何NSNumber的屬性,在DatabaseHelperI get this error.

insertNewEntityWithName方法:

+ (NSManagedObject *)insertNewEntityWithName:(NSString *)entityName 
            andContext:(NSManagedObjectContext *)managedObjectContext { 
    NSManagedObject *entity = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:managedObjectContext]; 
    return entity; 
} 

我想在我的模型,只拋出這個錯誤性質訪問的每一個屬性的類型的NSNumber的。其他人工作得很好。

+0

它們是否在您的模型中正確設置爲數字? –

+0

此外,您可以安全地將它們從NSNumber更改爲原始類型(Int,Double,模型中設置的任何值)的安全性。你所要做的就是改變財產和核心數據的類型。這可以使不必拆開NSNumbers更容易。 –

回答

0

顯然問題是我用手寫了模型的代碼。我通過自動生成代碼解決了這個問題。我通過在xcdatamodel中選擇我的實體,填寫數據模型檢查器中的Class Name(WeatherModel)和Codegen(Manual/None),然後選擇Editor> Create NSManagedObject Subclass。

相關問題