【发布时间】:2014-02-08 23:02:33
【问题描述】:
我有一个名为 HomeTableViewController 的 TableViewController,它加载了 Core Data 数组中的数据。我试图从选定的单元格中获取对象 ID,如下所示:
//Fetch Entity
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"OwedMoney"];
//Create an array that stores the contents of that entity
youOweArray = [[context executeFetchRequest:fetchRequest error:nil] mutableCopy];
//Create managedObjectId
NSManagedObject *YouOweData = [youOweArray objectAtIndex:0];
NSManagedObjectID *moID = [YouOweData objectID];
//Successfully got the managedObjectId
NSLog(@"This is the id %@", moID);
我能够获得 managedObjectId。但困难的部分来了。现在我想遍历我的数组并获取所有对象。然后我想获取等于所选单元格的对象并访问它的“付费”布尔值。我是这样做的:
BOOL Found = NO;
OwedMoney *OwedObject;
NSManagedObjectID *OwedId = [OwedObject objectID];
for (OwedMoney *OwedObject in youOweArray)
{
NSLog(@"%@", OwedObject);
if (OwedId == moID)
{
Found = YES;
break;
}
}
if (Found == YES)
{
BOOL isPaid = YES;
OwedMoney *Object = OwedObject;
Object.paid = [NSNumber numberWithBool:isPaid];
NSLog(@"Is it paid: %@",Object.paid? @"Yes":@"No");
}
虽然当我运行应用程序时,Found 永远不会等于 YES。这意味着我的对象 ID 不匹配。我想知道如何找到选定的表格视图单元格,从中获取 NSManagedObjectId,然后访问它的属性。
感谢所有帮助,提前致谢。
【问题讨论】:
标签: ios xcode core-data uitableview