Reputation: 11
In my core data entity, there is an attribute of Integer 32 type named validity.
There are two objects created earlier using this method:
- (void) AddMandatoryItems
{
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *newMonitorItem = [NSEntityDescription
insertNewObjectForEntityForName:@"MonitorItem"
inManagedObjectContext:context];
//
// Other codes here
//
[newMonitorItem setValue:[NSNumber numberWithInt:35]
forKey:@"validity"];
[self.monitorItemArray addObject:newMonitorItem];
NSError *error = nil;
if (![context save:&error])
{
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
//
// other codes here
//
[newMonitorItem setValue:[NSNumber numberWithInt:90]
forKey:@"validity"];
[self.monitorItemArray addObject:newMonitorItem];
if (![context save:&error])
{
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
// Return the number of rows in the section.
}
The validity values are 35 and 90 for the first and second objects respectively.
The two values are loaded like this:
- (void)viewDidLoad
{
//
// Other codes here
//
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"MonitorItem"];
self.monitorItemArray = [[managedObjectContext
executeFetchRequest:fetchRequest
error:nil] mutableCopy];
NSLog(@"viewDidLoad.monitorItemArray[1].validity: %d", (int)[self.monitorItemArray[1] valueForKey:@"validity"]);
}
the result from the NSLog is shown below:
2014-05-14 14:55:15.939 SGRecencyMonitor[3086:70b] viewDidLoad.monitorItemArray[1].validity: 1442
As shown the value should be 90 instead of 1442. However a check on the cellForRowAtIndexPath method below returns correct result:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSManagedObject *monitorItem = [self.monitorItemArray objectAtIndex:indexPath.row];
//
// code to display each row here
//
NSLog(@"ItemViewController tableview cellForRowAtIndexPath Validity: %@", [monitorItem valueForKey:@"validity"]);
return cell;
}
the result is as follow:
2014-05-14 14:55:15.948 SGRecencyMonitor[3086:70b] ItemViewController tableview cellForRowAtIndexPath Validity: 90
Can someone please explain to me why I cannot get the correct result using [self.monitorItemArray[1] valueForKey:@"validity"] I am using this method to pass the object self.monitorItemArray to another view. I can't retrieve the correct values through it. Below is the segue method
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"UpdateView"])
{
NSManagedObject *selectedItem = [self.monitorItemArray objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];
ItemUpdateViewController *destViewController = segue.destinationViewController;
destViewController.ItemReceived = selectedItem;
}
}
What have I done wrong? Thank you
Upvotes: 0
Views: 74
Reputation: 119292
In the first instance you're using %d
as a format specifier - this expects an integer, but you pass it an NSNumber
, which is an object. You're seeing the integer of the object's memory address. In the second instance you're using %@
, which expects an object, and you're seeing the result of the NSNumber
's description
method, which just prints out the number value.
Either change the first format specifier to %@
or pass in the integerValue
from the number object.
Upvotes: 1
Reputation: 2503
Try this:
- (void)viewDidLoad
{
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"MonitorItem"];
self.monitorItemArray = [[managedObjectContext
executeFetchRequest:fetchRequest
error:nil] mutableCopy];
NSNumber *n = [self.monitorItemArray[1] valueForKey:@"validity"];
NSLog(@"viewDidLoad.monitorItemArray[1].validity: %d", [n intValue]);
}
// it works for me
Upvotes: 0