gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 11:14:29 24 4
gpt4 key购买 nike

我正在使用带有 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com