我正在尝试创建一个非常简单的应用程序来操作iOS核心数据模型,我将其命名为'Person',其属性为:'age','firstName'和'lastName'。
然而,似乎只要我尝试获取或查询此数据模型,iOS似乎返回零值。
'NSInvalidArgumentException',原因:'+ entityForName:nil不是 合法的NSManagedObjectContext参数搜索实体名称 '人'“
这是我的视图控制器.m文件的样子:
#import "bitcraft_v1ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import "Person.h"
@interface bitcraft_v1ViewController ()
@end
@implementation bitcraft_v1ViewController
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
CLLocationManager *locationManager;
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
float latitude = locationManager.location.coordinate.latitude;
float longitude = locationManager.location.coordinate.longitude;
NSLog(@"This is latitude: %f", latitude);
NSLog(@"This is longitude: %f", longitude);
Person *newPerson = [NSEntityDescription
insertNewObjectForEntityForName:@"Person"
inManagedObjectContext:self.managedObjectContext];
}
我的视图控制器头文件如下所示:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@interface bitcraft_v1ViewController : UIViewController
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
//- (void)saveContext;
@property (strong, nonatomic) IBOutlet UILabel *regionCurrentlyEntered;
@end
最后我还没有触及Person.h和Person.m:
Person.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface Person : NSManagedObject
@property (nonatomic, retain) NSNumber * age;
@property (nonatomic, retain) NSString * firstName;
@property (nonatomic, retain) NSString * lastName;
@end
Person.m
#import "Person.h"
@implementation Person
@dynamic age;
@dynamic firstName;
@dynamic lastName;
@end
我知道我的视图控制器实现文件只声明变量person但从未实际存储过硬编码的名字,姓氏和年龄。但我似乎无法超越这一点。
为什么NSManagedObjectContext无法在核心数据中找到实体Person?最后声明'newPerson
'然后我能够创建“John Smith 42”并将此newPerson存储在- (void)viewDidLoad
内的核心数据中吗?