Reputation: 701
I have a TableViewController called HomeTableViewController loaded with data from a Core Data array. I attempted to get the object Id from the selected cell like so:
//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);
I was able to get the managedObjectId. But here comes the difficult part. Now I want to iterate through my array and get all the objects. Then I want to get the object that equals to the selected cell and access it's boolean value of "Paid". I did THAT like so:
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");
}
Although when I run the application, Found is never equal to YES. This means my object Id's aren't matching up. I'd like to know how of a way to find the selected table view cell, grab the NSManagedObjectId from it, and then access it's attributes.
All help is appreciated, Thanks in advance.
Upvotes: 0
Views: 4280
Reputation: 1
I think the problem is on line 9.
if(OweID == moID)
Probably the ID object (not the ID information itself) of the managed object changed. Using == compares only the references. Try to use
if([OweID isEqual::moID])
Thomas
Upvotes: 0
Reputation: 176
You're looping through the array, but never actually changing the OwedId to what item you're looking at in the array. This should work:
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");
}
Upvotes: 1