我有一个名为TableViewController的HomeTableViewController,它装载了来自Core数组的数据。我试图从选定的单元格中获取对象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。但困难的部分来了。现在,我想遍历我的数组并获取所有对象。然后,我希望得到与所选单元格相等的对象,并访问它的布尔值"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");
}尽管当我运行应用程序时,Found永远都不等于是。这意味着我的对象Id不匹配。我想知道如何找到所选的表视图单元格,从其中获取NSManagedObjectId,然后访问它的属性。
所有的帮助都很感激,谢谢。
发布于 2014-02-08 23:36:01
您正在遍历数组,但不要实际将OwedId更改为正在数组中查看的项。这应该是可行的:
BOOL Found = NO;
OwedMoney *Object;
for (OwedMoney *OwedObject in youOweArray)
{
NSManagedObjectID *OwedId = [OwedObject objectID];
NSLog(@"%@", OwedObject);
if (OwedId == moID)
{
Found = YES;
Object = OwedObject;
break;
}
}
if (Found)
{
BOOL isPaid = Found;
Object.paid = [NSNumber numberWithBool:isPaid];
NSLog(@"Is it paid: %@",Object.paid.boolValue? @"Yes":@"No");
}发布于 2017-02-09 23:05:45
我认为问题在第9行。
if(OweID == moID)可能已更改了托管对象的ID对象(而不是ID信息本身)。使用==只比较引用。试着使用
if([OweID isEqual::moID])托马斯
https://stackoverflow.com/questions/21652885
复制相似问题