hgc*_*hez 3 xcode ios swift ios8 xcode7
我想检索当前位置.我在swift Xcode 7上工作.我看了PLUSIEUR教程,但每次使用相同的方法.这是我的代码和我的错误:!
错误:无法使用索引类型int下标[CLPlacemark]类型的值
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
let LocationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.LocationManager.delegate = self
self.LocationManager.desiredAccuracy = kCLLocationAccuracyBest
self.LocationManager.requestWhenInUseAuthorization()
self.LocationManager.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]) {
CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: { (placemarks, error) -> Void in
if (error != nil) {
print("Error")
return
}
if placemarks!.count > 0 {
let pm = placemarks[0] as CLPlacemark
self.displayLocationInfo(pm)
}
else {
print("errorData")
}
})
}
func displayLocationInfo(placemark: CLPlacemark){
self.LocationManager.stopUpdatingLocation()
print(placemark.locality)
print(placemark.postalCode)
print(placemark.administrativeArea)
print(placemark.country)
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Error:" + error.localizedDescription)
}
}
Run Code Online (Sandbox Code Playgroud)
Rob*_*Rob 14
不,在Xcode 7中,错误是:
错误:无法下标类型'[CLPlacemark]?'的值 索引类型为'Int'
注意事项?.你必须打开那个可选项.所以你可以替换:
if placemarks!.count > 0 {
let placemark = placemarks[0] as CLPlacemark
self.displayLocationInfo(placemark)
}
Run Code Online (Sandbox Code Playgroud)
有:
if let placemark = placemarks?.first {
self.displayLocationInfo(placemark)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2588 次 |
| 最近记录: |