在Swift中,是否可以将另一个对象的函数强制转换为不同类型的对象?

时间:2018-02-18 19:53:38

标签: swift casting

我正在关注Ray Wenderlich的site上的MapKit教程,我对几行代码的说法有点困惑。

特别是在注释// 4下,开发人员似乎使用了一个等于mapView函数的常量变量,然后将其转换为MKMarkAnnotationView类型。

我从来没有见过这样的东西,但在继续前进之前我想先了解它。我理解函数也是对象,我理解可以在变量中放置一个函数,但是在这个例子中,开发人员不仅将函数放在变量中,而且开发人员还将它转换为另一种令人困惑的类型。这行代码可以分解为更小的步骤,以帮助我更好地理解它吗?

开发人员似乎调用了mapView对象,该对象的类型为MKMapView,但允许将其选择性地转换为MKMarkerAnnotationView类型。

//4
        if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKMarkerAnnotationView {
            dequeuedView.annotation = annotation
            view = dequeuedView
        }

如果需要,以下是viewController的完整代码:

import UIKit
import MapKit

class ViewController: UIViewController {

    //created an IBOutlet to control the mapView in interface builder
    @IBOutlet weak var mapView: MKMapView!

    override func viewDidLoad() {
    super.viewDidLoad()

        //initial location to zoom the map into once the app is opened.
        let initialLocation = CLLocation.init(latitude: 21.282778, longitude: -157.829444)

        centerMapOnLocation(location: initialLocation)

        mapView.delegate = self

        let artwork = Artwork.init(title: "King David Kalakaua", locationName: "Waikiki Gateway Park", discipline: "Sculpture", coordinate: CLLocationCoordinate2D.init(latitude: 21.283921, longitude: -157.831661))

        mapView.addAnnotation(artwork)

  }

    //when specifying a latlong to zoom into in iOS, you must also state a rectangular region for it to display a correct zoom level???
    let regionRadius: CLLocationDistance = 1000

    func centerMapOnLocation(location: CLLocation){
        let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius, regionRadius)
        mapView.setRegion(coordinateRegion, animated: true)
    }

}

extension ViewController: MKMapViewDelegate {

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        //2
        guard let annotation = annotation as? Artwork else {
            return nil
        }
        //3
        let identifier = "marker"
        var view: MKMarkerAnnotationView
        //4
        if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKMarkerAnnotationView {
            dequeuedView.annotation = annotation
            view = dequeuedView
        } else {
            //5
            view = MKMarkerAnnotationView.init(annotation: annotation, reuseIdentifier: identifier)
            view.canShowCallout = true
            view.calloutOffset = CGPoint.init(x: -5, y: 5)
            view.rightCalloutAccessoryView = UIButton.init(type: .detailDisclosure)
            view.markerTintColor = UIColor.green
        }
        return view
    }
}

1 个答案:

答案 0 :(得分:1)

这是可选的展开 正如您所注意到的那样 - 开发人员可以选择将函数的结果转换为MKMarkerAnnotationView。但他也使用了if let语法,这是可选的解包。这意味着这段代码

dequeuedView.annotation = annotation
view = dequeuedView
只有在演员成功时才会执行

(即如果演员表结果不是nil)。否则,此代码将被忽略。

您也可以使用guard语句执行此操作。 E.g:

guard let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKMarkerAnnotationView 
else { // code here will be executed if casting fails. In this case you also have to return function }

dequeuedView.annotation = annotation
view = dequeuedView

更多信息in documentation