Ios 如何使用NSManagedObjectContext显示值
我试图用NSManagedObjectContext显示对象的值,但我不知道该如何处理这段代码。我应该使用NSManagedModel还是其他什么Ios 如何使用NSManagedObjectContext显示值,ios,objective-c,Ios,Objective C,我试图用NSManagedObjectContext显示对象的值,但我不知道该如何处理这段代码。我应该使用NSManagedModel还是其他什么 -(NSManagedObjectContext *) managedObjectContext { NSManagedObjectContext *context = nil; id delegate = [[UIApplication sharedApplication] delegate]; if ([delegat
-(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控制器中。假设您希望显示阵列中第一个播放器的位置,并且该位置存储为字符串,则可以执行以下操作:
....
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];
....
locationLabel应该显示什么?这与PlayerArray有关吗?我在xcdatamodeid中声明了一个名为location的属性。PlayerArray应该包含位置。我只想在下面显示它,请参见我的答案,让我知道,如果有任何理由在任何地方使用NSManagedModel,除了在应用程序启动时设置核心数据之外,这是否非常、非常不寻常。sry这不起作用:我正在尝试这样做NSManagedObjectModel*playerModel=[self.playersArray objectAtIndex:indexath.row],但我不希望我的数据位于tableview中。这有意义吗?@user4567987查看我的编辑,让我知道这是否是你的意思