ios - 无法在 swift2 中声明类扩展

标签 ios swift swift2

我正在使用带有 swift 版本 2 的 xcode 7。我正在尝试按照 swift 教程中的教程进行操作,但代码是用 swift 1.2 编写的

当我添加类扩展 block 时出现错误:声明仅在文件范围内有效。这是扩展 block :

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
        }

我不知道如何解决这个问题,因为我以前没有使用过协议(protocol)或扩展。我应该使用 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
        }
    }

} 

最佳答案

Declaration is only valid at file scope

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

仅有效:所以,隐含地,如果你按照它说的去做,那么你的代码将编译

在文件范围内:即仅在文件的顶层。不在任何其他类、结构等中。

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

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
    }
}

关于ios - 无法在 swift2 中声明类扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32768452/

相关文章:

ios - 如何设置计时器来启用/禁用按钮

ios - swift 3 : present does not work on iOS 9 and older

ios - 当 BLE 设备在范围内时在后台启动应用程序

iOS 模糊效果到 ImageView 与 Swift

ios - SpriteKit 中 SKLabelNode 的物理体?

ios - 检测用户何时单击 UIWebView 或 WKWebView 中加载的 Web 表单

iphone - 如何从 animationDidStop 中删除 CALayer 对象?

json - 如何读取 blockerList.json

ios - 触摸 UITextView 时显示 PickerView

ios - 无法将 View 和图像定位在场景底部