简体   繁体   中英

Core Data: NSManagedObject adding values “forKey” - NSNumber truncates. Alternative?

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"];
}

为什么不使用[NSNumber numberWithDouble:x]

you might take a look at NSDecimalNumber ? Otherwise i believe you might have to deal with the rounding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM