Xcode 结果类型元素dos与预期类型Swift2不匹配
当我尝试运行代码时,我遇到以下错误。在那之前我一直在工作,我升级了Xcode并获得了用于IOS9的Xcode测试版7.0。现在要求我将代码转换为swift 2语法,这给了我一个错误。下面是我的代码片段Xcode 结果类型元素dos与预期类型Swift2不匹配,xcode,swift2,Xcode,Swift2,当我尝试运行代码时,我遇到以下错误。在那之前我一直在工作,我升级了Xcode并获得了用于IOS9的Xcode测试版7.0。现在要求我将代码转换为swift 2语法,这给了我一个错误。下面是我的代码片段 import UIKit import MapKit class MapViewController: UIViewController, MKMapViewDelegate { @IBOutlet var mapView:MKMapView! var restaurant:Restaura
import UIKit
import MapKit
class MapViewController: UIViewController, MKMapViewDelegate {
@IBOutlet var mapView:MKMapView!
var restaurant:Restaurant!
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
// Convert address to coordinate and annotate it on map
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(restaurant.location, completionHandler: { placemarks, error in
if error != nil {
print(error)
return
}
if placemarks != nil && placemarks!.count > 0 {
let placemark = placemarks[0] as! CLPlacemark
// Add Annotation
let annotation = MKPointAnnotation()
annotation.title = self.restaurant.name
annotation.subtitle = self.restaurant.type
annotation.coordinate = placemark.location.coordinate
self.mapView.showAnnotations([annotation], animated: true)
self.mapView.selectAnnotation(annotation, animated: true)
}
})
}
在Xcode 7.0中,placemarks数组不再是[AnyObject]?但现在是[位置]?。不需要强制转换数组。在swift代码中添加守卫声明:
import UIKit
import MapKit
class MapViewController: UIViewController, MKMapViewDelegate {
@IBOutlet var mapView:MKMapView!
var restaurant:Restaurant!
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
// Convert address to coordinate and annotate it on map
let geoCoder = CLGeocoder()
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let location = locations.last
geoCoder.reverseGeocodeLocation(manager.location!) { placemarks, error in
guard let placemarks = placemarks else { print(error); return; }
guard let placemark = placemarks.first else { return; }
let annotation = MKPointAnnotation()
annotation.title = self.restaurant.name
annotation.subtitle = self.restaurant.type
annotation.coordinate = placemark.location!.coordinate
self.mapView.showAnnotations([annotation], animated: true)
self.mapView.selectAnnotation(annotation, animated: true)
}
}
}