我有一个名为HomeTableViewController的TableViewController加载了来自Core Data数组的数据。我试图从选定的单元格中获取对象Id,如下所示:如何获得NSManagedObjectId和检索数据
//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。但是,这是困难的一部分。现在我想遍历我的数组并获取所有对象。然后我想获得等于所选单元格的对象并访问它的布尔值“Paid”。我这样做是这样的:
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");
}
虽然当我运行应用程序时,发现永远不会等于是。这意味着我的对象ID不匹配。我想知道如何找到选定的表视图单元格,从中获取NSManagedObjectId,然后访问它的属性。
所有帮助表示赞赏,在此先感谢。
我实现了这个和付费仍没有返回 – doc92606
啊请不要介意,在我的部分小错误,它的返回是的! – doc92606