mapkit 中未解析的标识符
unresolved identifier in mapkit
下面的代码是关于堆栈溢出的相同问题。但是看起来代码在 swift 中 2. 我为一些代码添加了图像 jj 但我收到了 1 条错误消息。错误消息是 var anView = thisMAP.dequeueReusableAnnotationView(withIdentifier: reuseId )
。错误消息指出 reuseId 是未解析的标识符 这是唯一的错误消息。如果这是固定的代码将编译。
import UIKit
import MapKit
class Annotation: NSObject, MKAnnotation
{
var coordinate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
var custom_image: Bool = true
var color: MKPinAnnotationColor = MKPinAnnotationColor.purple
}
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet var thisMAP: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
self.thisMAP.delegate = self;
let annotation = Annotation()
thisMAP.addAnnotation(annotation)
let annotation2 = Annotation()
annotation2.coordinate = CLLocationCoordinate2D(latitude: 0.0, longitude: 1.0)
annotation2.custom_image = false
thisMAP.addAnnotation(annotation2)
let annotation3 = Annotation()
annotation3.coordinate = CLLocationCoordinate2D(latitude: 1.0, longitude: 0.0)
annotation3.custom_image = false
annotation3.color = MKPinAnnotationColor.green
thisMAP.addAnnotation(annotation3)
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if (annotation is MKUserLocation) {
return nil
}
var anView = thisMAP.dequeueReusableAnnotationView(withIdentifier: reuseId )
if anView == nil {
if let anAnnotation = annotation as? Annotation {
if anAnnotation.custom_image {
let reuseId = "jj.png"
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView.image = UIImage(named:"jj.png")
}
else {
let reuseId = "pin"
let pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView.pinColor = anAnnotation.color
anView = pinView
}
}
anView.canShowCallout = false
}
else {
anView.annotation = annotation
在这一行中,reuseID 在此位置未知 - 仅在 if-else
块内,这就是错误 reuseId is a unresolved identifier
:
的原因
var anView = thisMAP.dequeueReusableAnnotationView(withIdentifier: reuseId )
你只能在定义它的范围内使用它。
将 reuseID
的定义放在这一行之前!
它应该与您在 if anAnnotation.custom_image {
中定义的 reuseId
相同
下面的代码是关于堆栈溢出的相同问题。但是看起来代码在 swift 中 2. 我为一些代码添加了图像 jj 但我收到了 1 条错误消息。错误消息是 var anView = thisMAP.dequeueReusableAnnotationView(withIdentifier: reuseId )
。错误消息指出 reuseId 是未解析的标识符 这是唯一的错误消息。如果这是固定的代码将编译。
import UIKit
import MapKit
class Annotation: NSObject, MKAnnotation
{
var coordinate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
var custom_image: Bool = true
var color: MKPinAnnotationColor = MKPinAnnotationColor.purple
}
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet var thisMAP: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
self.thisMAP.delegate = self;
let annotation = Annotation()
thisMAP.addAnnotation(annotation)
let annotation2 = Annotation()
annotation2.coordinate = CLLocationCoordinate2D(latitude: 0.0, longitude: 1.0)
annotation2.custom_image = false
thisMAP.addAnnotation(annotation2)
let annotation3 = Annotation()
annotation3.coordinate = CLLocationCoordinate2D(latitude: 1.0, longitude: 0.0)
annotation3.custom_image = false
annotation3.color = MKPinAnnotationColor.green
thisMAP.addAnnotation(annotation3)
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if (annotation is MKUserLocation) {
return nil
}
var anView = thisMAP.dequeueReusableAnnotationView(withIdentifier: reuseId )
if anView == nil {
if let anAnnotation = annotation as? Annotation {
if anAnnotation.custom_image {
let reuseId = "jj.png"
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView.image = UIImage(named:"jj.png")
}
else {
let reuseId = "pin"
let pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView.pinColor = anAnnotation.color
anView = pinView
}
}
anView.canShowCallout = false
}
else {
anView.annotation = annotation
在这一行中,reuseID 在此位置未知 - 仅在 if-else
块内,这就是错误 reuseId is a unresolved identifier
:
var anView = thisMAP.dequeueReusableAnnotationView(withIdentifier: reuseId )
你只能在定义它的范围内使用它。
将 reuseID
的定义放在这一行之前!
它应该与您在 if anAnnotation.custom_image {
reuseId
相同