无法下标索引类型为 int 的 [CLPlacemark] 类型的值
Cannot subscript a value of type [CLPlacemark] with an index type int
我想检索当前位置。我在 swift Xcode 7 工作。
我看了 PLUSIEUR 教程,但每次他们都使用相同的方法。
这是我的代码和我的错误:
!
Error : Cannot subscript a value of type [CLPlacemark] with an index
type int
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)
}
}
不,在 Xcode 7 中错误是:
error: cannot subscript a value of type '[CLPlacemark]?' with an index of type 'Int'
注释?
。你必须打开那个可选的。所以你可以替换:
if placemarks!.count > 0 {
let placemark = placemarks[0] as CLPlacemark
self.displayLocationInfo(placemark)
}
与:
if let placemark = placemarks?.first {
self.displayLocationInfo(placemark)
}
我也 运行 在几乎相同的情况下解决了这个问题。再次提示是它认为它没有拆包所以放一个 !对我有用。
试试这个。
let pm = placemarks![0] as CLPlacemark
if placemarks!.count > 0
{
var pm:CLPlacemark!
pm = placemarks![0] as CLPlacemark
//let pm:CLPlacemark = placemarks.
self.displayLocationInfo(pm)
let locality = (pm.locality != nil) ? pm.locality : ""
let sublocality = (pm.subLocality != nil) ? pm.subLocality : ""
let thoroughfare = (pm.thoroughfare != nil) ? pm.thoroughfare : ""
let country = (pm.country != nil) ? pm.country : ""
let administrativeArea = (pm.administrativeArea != nil) ? pm.administrativeArea : ""
print(pm.subLocality)
var annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = "\(thoroughfare) \(sublocality), \(locality)"
annotation.subtitle = "\(administrativeArea), \(country)";
self.mapViewForVisitLocation.addAnnotation(annotation)
}
它适用于 X-Code 7 和 Swift 2.0。
我想检索当前位置。我在 swift Xcode 7 工作。 我看了 PLUSIEUR 教程,但每次他们都使用相同的方法。 这是我的代码和我的错误: !
Error : Cannot subscript a value of type [CLPlacemark] with an index type int
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)
}
}
不,在 Xcode 7 中错误是:
error: cannot subscript a value of type '[CLPlacemark]?' with an index of type 'Int'
注释?
。你必须打开那个可选的。所以你可以替换:
if placemarks!.count > 0 {
let placemark = placemarks[0] as CLPlacemark
self.displayLocationInfo(placemark)
}
与:
if let placemark = placemarks?.first {
self.displayLocationInfo(placemark)
}
我也 运行 在几乎相同的情况下解决了这个问题。再次提示是它认为它没有拆包所以放一个 !对我有用。
试试这个。
let pm = placemarks![0] as CLPlacemark
if placemarks!.count > 0
{
var pm:CLPlacemark!
pm = placemarks![0] as CLPlacemark
//let pm:CLPlacemark = placemarks.
self.displayLocationInfo(pm)
let locality = (pm.locality != nil) ? pm.locality : ""
let sublocality = (pm.subLocality != nil) ? pm.subLocality : ""
let thoroughfare = (pm.thoroughfare != nil) ? pm.thoroughfare : ""
let country = (pm.country != nil) ? pm.country : ""
let administrativeArea = (pm.administrativeArea != nil) ? pm.administrativeArea : ""
print(pm.subLocality)
var annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = "\(thoroughfare) \(sublocality), \(locality)"
annotation.subtitle = "\(administrativeArea), \(country)";
self.mapViewForVisitLocation.addAnnotation(annotation)
}
它适用于 X-Code 7 和 Swift 2.0。