无法在swift2中声明类扩展

时间:2015-09-24 18:29:28

标签: ios swift swift2

我正在使用带有swift版本2的xcode 7.我正在尝试在swift教程中使用教程,但代码是用swift 1.2编写的

当我添加类扩展块时,我收到错误:声明仅在文件范围内有效。这是扩展块:

extension ViewController: MKMapViewDelegate {
        func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
        {
            // 1
            if let treasure = annotation as? Treasure {
            let view: MKPinAnnotationView
            // 2
            if let dequeueView = mapView.dequeueReusableAnnotationViewWithIdentifier("pin") as? MKPinAnnotationView {
            dequeueView.annotation = annotation
            view = dequeueView
        } else {
            view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")
            view.canShowCallout = true
            view.animatesDrop = false
            view.calloutOffset = CGPoint(x: -5, y: 5)
            view.rightCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as! UIView
            }

            view.pinColor = treasure.pinColor()
            // 6
            return view
            }
            return nil
        }

我不知道如何解决这个问题,因为之前我没有使用过协议或扩展。我应该使用swift 2.0以不同的方式编写它吗?

我的代码如下:

import UIKit
import MapKit



class ViewController: UIViewController, MKMapViewDelegate {


  @IBOutlet var mapView : MKMapView!

    var treasures: [Treasure] = []

    override func viewDidLoad() {

        super.viewDidLoad()
                self.mapView.delegate = self
                self.mapView.addAnnotations(self.treasures)

        self.treasures = [
            HistoryTreasure(what: "Google's first office",
                            year: 1999,
                            latitude: 37.44451, longitude: -122.163369),
            HistoryTreasure(what: "Facebook's first office",
                            year: 2005,
                            latitude: 37.444268, longitude: -122.163271),
            FactTreasure(what: "Stanford University",
                        fact: "Founded in 1885 by Leland Stanford.",
                        latitude: 37.427474, longitude: -122.169719),
            FactTreasure(what: "Moscone West",
                        fact: "Host to WWDC since 2003.",
                        latitude: 37.783083, longitude: -122.404025),
            FactTreasure(what: "Computer History Museum",
                        fact: "Home to a working Babbage Difference Engine.", latitude: 37.414371, longitude: -122.076817),
            HQTreasure(company: "Apple",
                        latitude: 37.331741, longitude: -122.030333),
            HQTreasure(company: "Facebook",
                        latitude: 37.485955, longitude: -122.148555),
            HQTreasure(company: "Google",
                        latitude: 37.422, longitude: -122.084),
        ]



    }

    extension ViewController: MKMapViewDelegate {
        func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
        {
            // 1
            if let treasure = annotation as? Treasure {
            let view: MKPinAnnotationView
            // 2
            if let dequeueView = mapView.dequeueReusableAnnotationViewWithIdentifier("pin") as? MKPinAnnotationView {
            dequeueView.annotation = annotation
            view = dequeueView
        } else {
            view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")
            view.canShowCallout = true
            view.animatesDrop = false
            view.calloutOffset = CGPoint(x: -5, y: 5)
            view.rightCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as! UIView
            }

            view.pinColor = treasure.pinColor()
            // 6
            return view
            }
            return nil
        }
    }

} 

1 个答案:

答案 0 :(得分:3)

  

声明仅在文件范围内有效

声明:指的是您的extension ViewController: MKMapViewDelegate

仅有效:所以,默示情况下,如果您按照说明那样做,那么您的代码将会编译

在文件范围:即仅在您的文件中的顶级。不在任何其他类,结构等中。

您目前在类的范围内声明了您的扩展名。扩展必须在文件范围内。因此,将课程内部的代码剪切并粘贴到课堂外:

import UIKit
import MapKit



class ViewController: UIViewController, MKMapViewDelegate {


  @IBOutlet var mapView : MKMapView!

    var treasures: [Treasure] = []

    override func viewDidLoad() {

        super.viewDidLoad()
                self.mapView.delegate = self
                self.mapView.addAnnotations(self.treasures)

        self.treasures = [
            HistoryTreasure(what: "Google's first office",
                            year: 1999,
                            latitude: 37.44451, longitude: -122.163369),
            HistoryTreasure(what: "Facebook's first office",
                            year: 2005,
                            latitude: 37.444268, longitude: -122.163271),
            FactTreasure(what: "Stanford University",
                        fact: "Founded in 1885 by Leland Stanford.",
                        latitude: 37.427474, longitude: -122.169719),
            FactTreasure(what: "Moscone West",
                        fact: "Host to WWDC since 2003.",
                        latitude: 37.783083, longitude: -122.404025),
            FactTreasure(what: "Computer History Museum",
                        fact: "Home to a working Babbage Difference Engine.", latitude: 37.414371, longitude: -122.076817),
            HQTreasure(company: "Apple",
                        latitude: 37.331741, longitude: -122.030333),
            HQTreasure(company: "Facebook",
                        latitude: 37.485955, longitude: -122.148555),
            HQTreasure(company: "Google",
                        latitude: 37.422, longitude: -122.084),
        ]



    }
} 


extension ViewController: MKMapViewDelegate {
    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
    {
        // 1
        if let treasure = annotation as? Treasure {
        let view: MKPinAnnotationView
        // 2
        if let dequeueView = mapView.dequeueReusableAnnotationViewWithIdentifier("pin") as? MKPinAnnotationView {
        dequeueView.annotation = annotation
        view = dequeueView
    } else {
        view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")
        view.canShowCallout = true
        view.animatesDrop = false
        view.calloutOffset = CGPoint(x: -5, y: 5)
        view.rightCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as! UIView
        }

        view.pinColor = treasure.pinColor()
        // 6
        return view
        }
        return nil
    }
}