ios CLLocation didUpdateLocations错误
我正在使用XCode7.2版本 我尝试使用locationManager委派功能ios CLLocation didUpdateLocations错误,ios,swift,cllocationmanager,Ios,Swift,Cllocationmanager,我正在使用XCode7.2版本 我尝试使用locationManager委派功能 func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { let location = locations.last as! CLLocation print("%f/%f",location.coordinate.latitude,location.coordinate.lon
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last as! CLLocation
print("%f/%f",location.coordinate.latitude,location.coordinate.longitude)
}
但我得到的错误如下:
Downcast from 'CLLocation?' to 'CLLocation' only unwraps optionals; did you mean to use '!'?
我不知道如何解决这个问题
swift代码变化更大
有人能帮我修理吗
谢谢
位置。最后一个
返回类型是CLLocation?
,这是因为数组可能根本没有元素。作为错误状态,您不必将可选类型强制转换为非可选类型,只需执行以下操作即可将其展开:
let location = locations.last!
请注意,使用上述方法会导致异常,并总是考虑使用可选的解包< /p>
if let location = locations.last{
print("%f/%f",location.coordinate.latitude,location.coordinate.longitude)
}
谢谢你的回复。如果我使用optional,我将得到其他错误:var location?:CLLocation=locations.last错误:显示:行上的连续语句必须用“;”分隔您以错误的方式使用了optional。它应该是
var位置:CLLocation?=对不起,我有其他错误…或者。。。。变量位置:CLLocation?=locations.last lngLb.text=String(格式:'%f',(位置?.coordinate.longitude)!)latLb.text=String(格式:'%f',(位置?.coordinate.latitude)!)它将显示错误:变量“location”从未变异;考虑改为“让”constant@dickfala检查编辑后的答案。这是一个可选的展开,我建议你