Ios 如何从swift 3中的注释pin获取所有信息
我想知道当点击时如何从swift中的注释中获取所有信息。 这篇文章是: 但这并不能回答我的问题。我对代码做了一些修改Ios 如何从swift 3中的注释pin获取所有信息,ios,swift3,mapkit,Ios,Swift3,Mapkit,我想知道当点击时如何从swift中的注释中获取所有信息。 这篇文章是: 但这并不能回答我的问题。我对代码做了一些修改 func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { if let annotationTitle = view.annotation?.title { if let annotationPhone = view.annotation?.phoneNumber as?
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
if let annotationTitle = view.annotation?.title {
if let annotationPhone = view.annotation?.phoneNumber as? MKAnnotationView {
print("User tapped on annotation with title: \(annotationPhone!)")
}
print("User tapped on annotation with title: \(annotationTitle!)")
}
}
创建pin码(如果有帮助):
let annotation = MKPointAnnotation()
annotation.coordinate = item.placemark.coordinate
annotation.title = item.name
annotation.subtitle = "Coffee Shop"
self.map.addAnnotation(annotation)
但我得到了一个错误:
Value of type 'MKAnnotation' has no member 'phoneNumber'
我做错了什么?只是子类
MKPointAnnotation
,属性名为phoneNumber
:
class PhoneAnnotation : MKPointAnnotation {
var phoneNumber: String?
}
在此之后,您可以通过如下注释获得phoneNumber
值(以及其他自定义值):
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
if let phoneNumber = (view.annotation as? PhoneAnnotation)?.phoneNumber {
}
}
as?MKAnnotationView
。您是否将自定义MKAnnotationView
或自定义MKAnnotation
与属性phoneNumber
一起使用?如果是,请使用此选项:if let annotationView as?MyCustomAnnotationView,让annotationPhone=view.annotation.phoneNumber{
,或者您使用的路径(例如,我假设它是MKAnnotationView
).我正在使用MKPointAnnotationCool,没有任何错误,但现在我有一个错误。if语句没有运行!为什么会这样?@Albert可能phoneNumber为零或annotation不是自定义类的实例Mabye..那么我该怎么办?谢谢您的帮助。