views:

214

answers:

1

Hi, my app needs to store latitude and longitude values parsed from XML into Core Data. Currently, I have a Core Data Model which has these attributes set to type double. The header file for the Model Object has them defined as NSNumber

How can I convert the string stored in my CurrentNodeText variable into the required type for storage in Core Data? Also, is this an appropriate way to store coordinate data in Core Data?

+4  A: 

NSString has a doubleValue method that will examine the string to see if it looks like a double and return that value if so (or 0.0 if not).

As for whether that's an appropriate way to store coordinate data: It depends. Binary floating-point numbers cannot precisely represent every number that decimal numbers can. In these cases, they will store something that's excruciatingly close but which might screw up calculations demanding precision — for example, comparing floats for equality and testing whether one float is less than another will not yield exact results. If the precise number is important, you might prefer to keep the number in an NSDecimalNumber, NSDecimal or NSString.

Chuck
+1 for mentioning decimals. Floating-point datatypes are overused as it is.
Seva Alekseyev