无法在 MKAnnotationView 上转换子类
Cant cast subclass on MKAnnotationView
我的 FBSingleClusterView
中有一个名为 companyString
的字符串。但是我似乎无法访问,因为 MKAnnotationView
不会更改为我的 FBSingleClusterView
?
我的代码
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
pinView = FBSingleClusterView(annotation: annotation, reuseIdentifier: reuseId) as FBSingleClusterView
pinView.companyString = singleAnnotation.name
}
但是我不断收到以下错误 value of type MKAnnotationView has no member companyString
。
为什么不将其转换为 FBSingleClusterView
,它是 MKAnnotationView 的子类?
FBSingleClusterView
class FBSingleClusterView: MKAnnotationView {
var bubbleView: BubbleView?
var addressString: String?
var companyString: String?
var logoImage: UIImage?
override init(annotation: MKAnnotation?, reuseIdentifier: String?){
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
// change the size of the cluster image based on number of stories
backgroundColor = UIColor.clearColor()
setNeedsLayout()
}
required override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func layoutSubviews() {
// Images are faster than using drawRect:
centerOffset = CGPointMake(0, -image!.size.height/2);
}
}
试试这个(我在 "test" 上替换了你的变量):
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
var pinView: FBSingleClusterView! = nil
if let reuseView = mapView.dequeueReusableAnnotationViewWithIdentifier("test") as? FBSingleClusterView {
pinView = reuseView
} else {
pinView = FBSingleClusterView(annotation: annotation, reuseIdentifier: "test")
}
pinView.companyString = "Test"
return pinView
}
你的问题出在类型转换上
你的第一行是
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
因此,pinView 现在是类型 MKAnnotationView?
Why come it isn't being casted into a FBSingleClusterView, which is a subclass of the MKAnnotationView?
一旦设置了 var 类型,就不能重新分配其他类型。
建议的解决方案是
if let pinView:FBSingleClusterView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? FBSingleClusterView
{
pinView.companyString = singleAnnotation.name
}
我的 FBSingleClusterView
中有一个名为 companyString
的字符串。但是我似乎无法访问,因为 MKAnnotationView
不会更改为我的 FBSingleClusterView
?
我的代码
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
pinView = FBSingleClusterView(annotation: annotation, reuseIdentifier: reuseId) as FBSingleClusterView
pinView.companyString = singleAnnotation.name
}
但是我不断收到以下错误 value of type MKAnnotationView has no member companyString
。
为什么不将其转换为 FBSingleClusterView
,它是 MKAnnotationView 的子类?
FBSingleClusterView
class FBSingleClusterView: MKAnnotationView {
var bubbleView: BubbleView?
var addressString: String?
var companyString: String?
var logoImage: UIImage?
override init(annotation: MKAnnotation?, reuseIdentifier: String?){
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
// change the size of the cluster image based on number of stories
backgroundColor = UIColor.clearColor()
setNeedsLayout()
}
required override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func layoutSubviews() {
// Images are faster than using drawRect:
centerOffset = CGPointMake(0, -image!.size.height/2);
}
}
试试这个(我在 "test" 上替换了你的变量):
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
var pinView: FBSingleClusterView! = nil
if let reuseView = mapView.dequeueReusableAnnotationViewWithIdentifier("test") as? FBSingleClusterView {
pinView = reuseView
} else {
pinView = FBSingleClusterView(annotation: annotation, reuseIdentifier: "test")
}
pinView.companyString = "Test"
return pinView
}
你的问题出在类型转换上
你的第一行是
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
因此,pinView 现在是类型 MKAnnotationView?
Why come it isn't being casted into a FBSingleClusterView, which is a subclass of the MKAnnotationView?
一旦设置了 var 类型,就不能重新分配其他类型。
建议的解决方案是
if let pinView:FBSingleClusterView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? FBSingleClusterView
{
pinView.companyString = singleAnnotation.name
}