In trying to change all my objective C code into Swift (which is a pretty steep learning curve in itself), I've hit a problem.
I'm simply trying to save a CLLocationDegrees value into Core Data. But nothing I do is working.
I started with:
self.myLocation?.locationCurrentLat = self.fixedLocation?.coordinate.latitude
But have no idea how to get the CLLocationDegrees to downcast (if that's the right thing) to a Double or NSNumber and nothing I can search on Google is helping!
I'm still obviously foggy about lots of things. This is certainly one of them.
What might I be doing wrong ... or need to do?
Thanks in advance
CLLocationDegrees is a double. You shouldn't need to do anything.
If you do need to cast it to a double, use the syntax
Double(self.fixedLocation?.coordinate.latitude ?? 0)
But that should not be needed because CLLocationDegrees IS a type alias for a double.
To convert to an NSNumber, you'd use
NSNumber(value: self.fixedLocation?.coordinate.latitude ?? 0)
I edited the code above to use the "nil coalescing operator" to give the value 0 if self.fixedLocation
is nil. It would be safer to make it return an optional Int that contains a nil if the fixedLocation
is nil:
let latitude: Double?
if let location = self.fixedLocation {
latitude = Double(location.coordinate.latitude)
} else {
latitude = nil
}
Here's how to convert to NSNumber in Objective-C;
[NSNumber numberWithDouble:newLocation.coordinate.longitude]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With