我的核心数据中有一个名为Weather的实体,格式如下:
#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
我将此模型的实例保存到Core Data,如下所示:
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];
但是每当我尝试访问任何NSNumber属性时(甚至在调用saveCoreData方法之后), I get this error.
DatabaseHelper中的insertNewEntityWithName方法:
+ (NSManagedObject *)insertNewEntityWithName:(NSString *)entityName
andContext:(NSManagedObjectContext *)managedObjectContext {
NSManagedObject *entity = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:managedObjectContext];
return entity;
}
我尝试访问模型中的每个属性,只有抛出此错误的属性属于NSNumber类型。其他人工作得很好。
答案 0 :(得分:0)
显然问题是我手工编写了模型的代码。我通过自动生成代码解决了这个问题。我通过在xcdatamodel中选择我的实体,在数据模型检查器中填写类名(WeatherModel)和Codegen(手动/无),然后选择编辑器&gt;创建NSManagedObject子类来实现这一点。