我有一個名爲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