尝试访问核心数据模型的 NSNumber 属性 给出 EXC_BAD_ACCESS

Trying to access Core Data Model's NSNumber property gives 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];

但是,每当我尝试访问任何 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 类型。其他的都很好。

显然问题出在我手写了模型的代码。我通过自动生成代码解决了这个问题。我通过在 xcdatamodel 中选择我的实体,在数据模型检查器中填写 Class Name(WeatherModel) 和 Codegen(Manual/None) 然后选择 Editor>Create NSManagedObject Subclass.

来做到这一点