Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何使用MKAnnotation存储属性_Swift_Mkannotation - Fatal编程技术网

Swift 如何使用MKAnnotation存储属性

Swift 如何使用MKAnnotation存储属性,swift,mkannotation,Swift,Mkannotation,我正在使用这条线创建带有自定义接点的地图 self.mapView.addAnnotation(customMapAnnotationView.annotation!) 但我需要稍后遍历所有管脚,并将它们与一个键匹配,该键将标识是否要删除它们 for annotation:ChatRoomMapAnnotationView in self.chatMapView.annotations { ... } 如何将该键与注释一起存储,以便在迭代时可用 谢谢如果我理解正确,如果您希望在注释中存储额

我正在使用这条线创建带有自定义接点的地图

self.mapView.addAnnotation(customMapAnnotationView.annotation!)
但我需要稍后遍历所有管脚,并将它们与一个键匹配,该键将标识是否要删除它们

for annotation:ChatRoomMapAnnotationView in self.chatMapView.annotations {
 ...
}
如何将该键与注释一起存储,以便在迭代时可用


谢谢

如果我理解正确,如果您希望在注释中存储额外信息,您需要创建一个符合MKAnnotation的类,如下所示

class ChatRoomMapAnnotationView: NSObject, MKAnnotation {

let myKeyIdentifier: String
let coordinate: CLLocationCoordinate2D
let title: String?
let subtitle: String?

init(myKeyIdentifier: String, coordinate: CLLocationCoordinate2D, title: String?=nil, subtitle: String?=nil ) {
    self.myKeyIdentifier = myKeyIdentifier
    self.coordinate = coordinate
    self.title = title
    self.subtitle = subtitle
}
}

self.mapView.addAnnotation(CustomMapAnnotationView(.....))

for annotation in self.chatMapView.annotations {
    if let chatAnnotation = annotation as? ChatRoomMapAnnotationView {
       if chatAnnotation.myKeyIdentifier == "specialKey" {
          // do something special
       }
    }
}

您需要有用于注释的自定义类:

class CustomPin: MKPointAnnotation {
   var yourVar1: yourType1
   var yourVar2: yourType2
   // ...
   var yourVar3: yourType3
}
然后您可以这样使用它:

if annotation is CustomPin {
     // do something
}
希望能有帮助