如何解决:无法将"CLLocationDegrees"类型的值(也称为"Double")分配为"Float!"类型在Swift 3中 [英] How to Fix: Cannot assign value of type 'CLLocationDegrees' (aka 'Double') to type 'Float!' in Swift 3

查看:414
本文介绍了如何解决:无法将"CLLocationDegrees"类型的值(也称为"Double")分配为"Float!"类型在Swift 3中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Google地图上获得了一个坐标,我想将其分配给一个float变量,但是它显示了此错误:

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

无法将类型"CLLocationDegrees"(也称为"Double")的值分配给类型 '漂浮!'

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

推荐答案

将您的值转换为浮点,然后赋值

Convert your value to float and then assign

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

这篇关于如何解决:无法将"CLLocationDegrees"类型的值(也称为"Double")分配为"Float!"类型在Swift 3中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆