Search code examples
iosswiftlocationcoordinatecllocationcoordinate2d

How to Fix: Cannot assign value of type 'CLLocationDegrees' (aka 'Double') to type 'Float!' in Swift 3


I get a coordinate from my google map and I want to assign it to a float variable, but it shows this error:

Cannot assign value of type 'CLLocationDegrees' (aka 'Double') to type 'Float!'

func markerButtonClicked(sender:MapButton) {
 let coord = self.getCenterCoordinate()
    let addressObj = Address()
    addressObj.latitude  = coord.latitude 
    addressObj.longitude = coord.longitude
}
func getCenterCoordinate() -> (CLLocationCoordinate2D) {
    let location = CGPoint(x:self.mView.bounds.size.width/2,y:self.mView.bounds.size.height/2)
    let coord = self.mView.projection .coordinate(for: location)
    return coord
}    

class Address: NSObject {
    var latitude:Float!
    var longitude:Float!
}

Solution

  • Convert your value to float and then assign

    addressObj.latitude  = Float(coord.latitude)
    addressObj.longitude = Float(coord.longitude)