我试图用NSManagedObjectContext显示一个对象的值,但我不知道该怎么做这个代码。我应该使用NSManagedModel还是其他什么?
-(NSManagedObjectContext *) managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]initWithEntityName:@"Match"];
self.playersArray = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy
//What should I do here?
UILabel *locationLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 1000, 20)];
[locationLabel setText: //trying to get this];
[self.view addSubview:locationLabel];
}
我是xcode的初学者,对核心数据知之甚少。任何帮助表示赞赏。
我想做这样的事情:
NSManagedObjectModel *playerModel = [self.playersArray objectAtIndex:indexPath.row]
然后执行:
[playerModel valueForKey: @"location"];
但我希望我的数据显示在视图控制器中而不是tableview控制器中。
答案 0 :(得分:0)
假设您要显示数组中第一个玩家的位置并且该位置存储为字符串,您可以这样做:
....
self.playersArray = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy
NSManagedObjectModel *playerModel = [self.playersArray objectAtIndex:0]
UILabel *locationLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 1000, 20)];
[locationLabel setText:playerModel.location];
[self.view addSubview:locationLabel];
....