用戶位置這是我的代碼:獲得在迅速
import Foundation
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
@IBOutlet weak var locationLabel: UILabel!
var coord: CLLocationCoordinate2D?
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized {
locationManager.startUpdatingLocation()
println(coord!.longitude)
locationLabel.text = "location found"
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
var locationArray = locations as NSArray
var locationObj = locationArray.lastObject as CLLocation
coord = locationObj.coordinate
}
}
}
此代碼不返回任何東西(它應該打印經度)。這似乎O.K因爲如果我移動
locationManager.startUpdatingLocation()
println(coord!.longitude)
出來的if語句,Xcode中引發運行時錯誤,並說,它意外地發現零而展開的可選值。我不明白爲什麼?系統要求我允許我使用我的位置。
謝謝!