Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ios CLLocation didUpdateLocations错误_Ios_Swift_Cllocationmanager - Fatal编程技术网

ios CLLocation didUpdateLocations错误

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

我正在使用XCode7.2版本

我尝试使用locationManager委派功能

 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?=