Core Data: NSManagedObject adding values “forKey” - NSNumber truncates. Alternative?
-
26-09-2019 - |
Question
I understand that NSNumber cannot go beyond 1/10ths place. What can I use as an alternative to add a doubles (longitude/latitude) into Core Data? Using double directly does not work and NSNumber truncates the value.
NSManagedObject *newManagedObject = [NSEntityDescription
insertNewObjectForEntityForName:[entity name]
inManagedObjectContext:managedObjectContext];
if ([locObj valueForKey:@"latitude"] != NULL) {
NSNumber *latitude = [NSNumber numberWithInt:[[locObj valueForKey:@"latitude"] doubleValue]];
[newManagedObject setValue:latitude forKey:@"latitude"];
}
if ([locObj valueForKey:@"longitude"] != NULL) {
NSNumber *longitude = [NSNumber numberWithInt:[[locObj valueForKey:@"longitude"] doubleValue]];
[newManagedObject setValue:longitude forKey:@"longitude"];
}
Solution
Why don't you use [NSNumber numberWithDouble:x]
?
OTHER TIPS
you might take a look at NSDecimalNumber
? Otherwise i believe you might have to deal with the rounding.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow