How do I convert NSMutableString to double for storing in Core Data?
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 NSN开发者_如何学Cumber
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?
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.
精彩评论