Store high latitude / longitude values ​​in iOS master data

I am trying to store latitude / longitude in master data. They ultimately have an accuracy of 6 to 20 digits.

And for some reason, I used them as floating in Core Data, rounding them up and not giving me exact values. I tried the "decimal" type, and no luck.

Is NSStrings my only other option?

EDIT

NSManagedObject:

@interface Event : NSManagedObject { } @property (nonatomic, retain) NSDecimalNumber * dec; @property (nonatomic, retain) NSDate * timeStamp; @property (nonatomic, retain) NSNumber * flo; @property (nonatomic, retain) NSNumber * doub; 

Here is the code for the sample number that I store in the master data:

 NSNumber *n = [NSDecimalNumber decimalNumberWithString:@"-97.12345678901234567890123456789"]; 

The above value is printed. Sweet, the value I was expecting:

 Printing description of n: -97.12345678901234567890123456789 

The code to access it again:

 NSNumber *n = [managedObject valueForKey:@"dec"]; NSNumber *f = [managedObject valueForKey:@"flo"]; NSNumber *d = [managedObject valueForKey:@"doub"]; 

Printed Values:

 Printing description of n: -97.1234567890124 Printing description of f: <CFNumber 0x603f250 [0xfef3e0]>{value = -97.12345678901235146441, type = kCFNumberFloat64Type} Printing description of d: <CFNumber 0x6040310 [0xfef3e0]>{value = -97.12345678901235146441, type = kCFNumberFloat64Type} 
+8
ios iphone core-location
source share
7 answers

Do you use the NSNumber shell?

Configure the repository to use NSNumber instead of float or decimal and use this to save the coordinates:

 [NSNumber numberWithDouble:coordinate.latitude] //Same for longitude. 
+10
source share

Try using the Double data type in Core Data. Since the coordinates of your location are doubled, it makes sense to use them in Core Data. Although it may be available only now (iOS 5).

+1
source share

When Core Data stores data in SQLite, it uses numeric columns. SQLite stores numbers as - at most - 8-byte values, whether integer or floating point. Thus, while NSDecimalNumber will be very happy to accurately represent these coordinate values, they can be disabled using the decimal Core Data attribute supported by SQLite.

+1
source share

since the coordinate ranges are well defined, you can add a few digits of accuracy (compared to double) using the 64-bit int representation (or even several).

in fact, your sources and goals may not use or provide such high accuracy, therefore ... there may not be so many.

0
source share

I believe that the truncation problem may be related to the registration, and not to the actual data stored in Core Data.

Can you confirm the sending of the code that you use to register the output?

I am talking about this because I noticed that when registering some of my longer NSString fields, the Core Data description will only display about 50 characters, which may make you think that this is data truncation, but actually it is just a truncation of its description.

0
source share

In the data model editor, make sure your lat / long fields are set to double . Otherwise, CoreData will perform the conversion for you, and the result will not be what you expect (in this case, decimals will be deleted).

0
source share

We strongly recommend using NSDecialNumber for storage if you need an exact value or comparison in your code. you can have a different value after saving and get the value back from the kernel data in double fields.

0
source share

Source: https://habr.com/ru/post/650631/


All Articles