我无法访问我的NSManagedObject的属性。我看到了这个对象,可以访问viewDidLoad
中的属性,然后访问viewWillAppear
中的其他方法,我仍然会看到此对象,但在调用属性后获取null
。
@interface ListsViewController()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *lists;
@end
@implementation ListsViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSManagedObjectContext *managedObjectContext = [[CoreDataController sharedInstance] newManagedObjectContext];
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"List"];
NSError *error = nil;
NSArray *result = [managedObjectContext executeFetchRequest:request error:&error];
if (!result) {
NSLog(@"Failure fetch: %@", error);
} else {
self.lists = result;
NSLog(@"%@", self.lists);
List *list = [self.lists firstObject];
NSLog(@"%@", list.name);
}
self.tableView.delegate = self;
// self.tableView.dataSource = self;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"%@", self.lists);
List *list = [self.lists firstObject];
NSLog(@"%@", list.name);
}
控制台:
(
"<List: 0x7fb5b9e6b7c0> (entity: List; id: 0xd000000000040000 <x-coredata://D831EA3F-2A96-4AA8-B9CF-BFFEB0B6D611/List/p1> ; data: <fault>)",
"<List: 0x7fb5b9e53700> (entity: List; id: 0xd000000000080000 <x-coredata://D831EA3F-2A96-4AA8-B9CF-BFFEB0B6D611/List/p2> ; data: <fault>)"
)
Underway
(
"<List: 0x7fb5b9e6b7c0> (entity: List; id: 0xd000000000040000 <x-coredata://D831EA3F-2A96-4AA8-B9CF-BFFEB0B6D611/List/p1> ; data: <fault>)",
"<List: 0x7fb5b9e53700> (entity: List; id: 0xd000000000080000 <x-coredata://D831EA3F-2A96-4AA8-B9CF-BFFEB0B6D611/List/p2> ; data: <fault>)"
)
(null)
我做错了什么?
更新
我的错误是我没有持有managedObjectContext:
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
- (void)viewDidLoad {
self.managedObjectContext = [[CoreDataController sharedInstance] newManagedObjectContext];
}
现在工作正常。