我想访问我编写的父注释类的属性以检查它的值。
Annotation.swift
enum AnnotationType: Int {
case AnnotationDefault = 0
case AnnotationAED
case AnnotationResponder
case AnnotationIncident
}
class Annotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
var subTitle: String
var type: AnnotationType
init(coordinate: CLLocationCoordinate2D, title: String, subTitle: String, type: AnnotationType) {
self.coordinate = coordinate
self.title = title
self.subTitle = subTitle
self.type = type
}
func getType() -> AnnotationType {
return self.type
}
}
AEDAnnotation.swift
class AEDAnnotation : Annotation {
let aed: AED
init(aed: AED) {
self.aed = aed
super.init(coordinate: aed.coordinate, title: aed.street, subTitle: aed.owner, type: aed.annotationType)
}
func getAnnotationType() -> AnnotationType {
return super.getType()
}
}
我创建了一个这样的注释:
let annotation = AEDAnnotation.init(aed: aed)
self.annotationArray.append(annotation)
如果我遍历数组,我看到那里有有效的AEDAnnotations。但是为什么我无法访问我要求的Annotation.swift
的基础属性。
for item in self.annotationArray {
print(item.getType)
}
这不起作用。但是,如何才能访问type
Annotation.swift
我得到的错误信息是:
Value of type 'MKAnnotation' has no member 'getType'
答案 0 :(得分:1)
该错误表示item
属于MKAnnnotation
类型。如果您想将其视为不同的类型,则需要将其强制转换。请尝试以下方法:
for item in self.annotationArray {
if let myAnnotation = item as Annotation {
print("\(myAnnotation.getType().rawValue)")
}
else {
print("Annotation was not an Annotation type")
}
}