2017-01-26 63 views
0

以下代碼是堆棧溢出時的相同問題。然而,它看起來像代碼在迅速2.我添加了一些代碼的圖像jj,但我收到1錯誤消息。錯誤消息是var anView = thisMAP.dequeueReusableAnnotationView(withIdentifier: reuseId)。錯誤消息指出reuseId是未解決的標識符這是唯一的錯誤消息。如果這是固定的,代碼將被編譯。mapkit中的未解析標識符

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 

回答

0
在此行中reuseID不是在這個地方稱爲

- 只有if-else塊這就是爲什麼它是錯誤reuseId is a unresolved identifier內:

var anView = thisMAP.dequeueReusableAnnotationView(withIdentifier: reuseId) 

它在哪裏,你只能用它的範圍內定義。

在此行之前放置reuseID的定義!

它應該是一樣的reuseId您在if anAnnotation.custom_image {

+0

定義什麼將定義爲reuselID? –

+0

更新了問題 – muescha