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

人走茶凉 提交于 2020-01-24 09:48:26

问题


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!
}

回答1:


Convert your value to float and then assign

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


来源:https://stackoverflow.com/questions/42364537/how-to-fix-cannot-assign-value-of-type-cllocationdegrees-aka-double-to-ty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!