简体   繁体   English

Canot将类型“ UIView”的值分配给类型-符合UIView的类-

[英]Canot assign value of type 'UIView' to type --class conforming to UIView--

I have the following class conforming to UIView: 我有以下符合UIView的类:

import UIKit

class LocationInformationCalloutView: UIView {
      :
      :

Then I have a second class that looks like this: 然后,我有第二堂课,看起来像这样:

class LocationInformationAnnotationView: MKAnnotationView {
    weak var customCalloutView : LocationInformationCalloutView?
        }
    }
        :
        :

So you can see I have a variable called customAnnotationView that is of type LocationInformationCalloutView which is of type UIView 这样您就可以看到我有一个名为customAnnotationView的变量,该变量的类型为LocationInformationCalloutView ,其类型为UIView

The loadLocationInformationCalloutView() function looks like this (just a function that returns an UIView): loadLocationInformationCalloutView()函数如下所示(只是一个返回UIView的函数):

func loadLocationInformationCalloutView() -> UIView? {
    let view = UIView(frame: CGRect(x: 0, y: 0, width: 240, height: 280))
    return view
}

However upon invoking this line of code: 但是,在调用以下代码后:

self.customCalloutView = newCustomCalloutView

within this block of code: 在此代码块中:

 override func setSelected(_ selected: Bool, animated: Bool) {
    if selected {
        self.customCalloutView?.removeFromSuperview()
        if let newCustomCalloutView = loadLocationInformationCalloutView() {
            newCustomCalloutView.frame.origin.x -= newCustomCalloutView.frame.width / 2.0 - (self.frame.width / 2.0)
            newCustomCalloutView.frame.origin.y -= newCustomCalloutView.frame.height
            self.addSubview(newCustomCalloutView)
            self.customCalloutView = newCustomCalloutView
            if animated {
                self.customCalloutView!.alpha = 0.0
                UIView.animate(withDuration: 1.8, animations: {
                    self.customCalloutView!.alpha = 1.0
                })

I get the following error: 我收到以下错误:

Cannot assign value of type 'UIView' to type 'LocationInformationnCalloutView?' 无法将类型“ UIView”的值分配给类型“ LocationInformationnCalloutView?”

Can someone shed some light on this and help me through this problem? 有人可以阐明这一点并帮助我解决这个问题吗? Any help si greatly appreciated, thanks! 非常感谢任何帮助,谢谢!

LocationInformationCalloutView inherits from UIView , which means that you can assign an instance of LocationInformationCalloutView to a property whose type is UIView , but you cannot do so the other way around. LocationInformationCalloutView继承自UIView ,这意味着您可以将LocationInformationCalloutView的实例分配给类型为UIView的属性,但不能这样做。

At the line self.customCalloutView = newCustomCalloutView you are trying to assign a UIView instance to a property of type LocationInformationCalloutView , which cannot work, since a parent class cannot be used in place of a child instance. self.customCalloutView = newCustomCalloutView您试图将UIView实例分配给LocationInformationCalloutView类型的属性,该属性无法正常工作,因为不能使用父类代替子实例。

You need to change the return type of loadLocationInformationCalloutView() to be LocationInformationCalloutView instead of UIView . 您需要将loadLocationInformationCalloutView()的返回类型更改为LocationInformationCalloutView而不是UIView

func loadLocationInformationCalloutView() -> LocationInformationCalloutView {
    return LocationInformationCalloutView(frame: CGRect(x: 0, y: 0, width: 240, height: 280))
}

You got it the other way around. 您反过来了。 You can assign a variable of a subclass to a variable of a superclass, but you can't do the opposite for obvious reasons. 您可以将子类的变量分配给超类的变量,但是出于明显的原因,您不能做相反的事情。

First thing, you method should be return ing a valid LocationInformationCalloutView if it is supposed to that. 首先,您的方法应该返回一个有效的LocationInformationCalloutView If you have to, for some reason return it as a UIView . 如果有必要,出于某种原因将其作为UIView返回。 Then you have to cast it to LocationInformationCalloutView before saving it in customCalloutView . 然后,必须先将其转换为LocationInformationCalloutView然后再将其保存到customCalloutView

if let validCustomCalloutView = newCustomCalloutView as? LocationInformationCalloutView {
    self.customCalloutView = validCustomCalloutView
}

Note: The casting will fail if the UIView being passed is not actually an instance of LocationInformationCalloutView . 注意:如果传递的UIView实际上不是LocationInformationCalloutView的实例,则转换将失败。 The method should preferably look like how @DávidPásztor answer shows. 该方法最好看起来像@DávidPásztor答案所示。

Did you try to instantiate the CalloutView with its own classtype like this: 您是否尝试使用其自己的类类型实例化CalloutView,如下所示:

func loadLocationInformationCalloutView() -> LocationInformationCalloutView? {
let view = LocationInformationCalloutView(frame: CGRect(x: 0, y: 0, width: 240, height: 280))
return view

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM