You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS自定义CastingPromptView编译报类型不匹配、无成员错误

编译自定义AlertView遇到的类型错误

报错信息

编译自定义CastingPromptView相关代码时触发两个编译错误:

Value of type 'CastingPromptViewDelegate' has no member 'removeFromSuperview'

'removeFromSuperview' Argument type 'CastingPromptView' does not conform to expected type 'CastingPromptViewDelegate'

问题代码

涉及的核心实现代码如下:

// WatchVC中的懒加载视图定义
private lazy var castingPromptView: CastingPromptView = {
    let view = CastingPromptView.instanceFromNib(castDeviceName: "test", imageURLString: "https://dsfsdf.jpg")
    view.translatesAutoresizingMaskIntoConstraints = false
    view.delegate = self
    return view
}()


// 自定义CastingPromptView实现
class CastingPromptView: UIView{
    // MARK: - IBOutlets
    @IBOutlet weak var castTODeviceLabel: UILabel!
    @IBOutlet weak var videoImage: URLImageView?
    @IBOutlet weak var playCastingButton: UIButton!
    @IBOutlet weak var stopCastingButton: UIButton!
    @IBOutlet weak var cancelButton: UIButton!
    
    // MARK: - Properties
    public var delegate: CastingPromptViewDelegate?
    
    class func instanceFromNib(castDeviceName:String,imageURLString:String) -> CastingPromptView {
        let view = UINib(nibName: "CastingPromptView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CastingPromptView
        view.castTODeviceLabel.text = castDeviceName
        view.setupView()
        return view
    }
    
    override init(frame: CGRect) {
      super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
      super.init(coder: aDecoder)
      setupView()
    }
    
    private func setupView() {
        self.backgroundColor = .white
        self.layer.borderColor = UIColor(red: 0.98, green: 0.98, blue: 0.98, alpha: 1.00).cgColor
        self.layer.borderWidth = 1
        self.layer.cornerRadius = 12
    }
    
    @IBAction func dismissView(_ sender: Any) {
      // 此处传入self即CastingPromptView实例,和协议定义的参数类型不匹配
      self.delegate?.dismissAlert(sender: self)
    }
    
    @IBAction func stopCasting(_ sender: Any) {}
    
    @IBAction func playCasting(_ sender: Any) {}
 }

// MARK: - Delegate 协议定义
protocol CastingPromptViewDelegate {
    // 错误点:参数类型被定义为协议类型
    func dismissAlert(sender: CastingPromptViewDelegate)
}


extension WatchVC:CastingPromptViewDelegate {
    func dismissAlert(sender: CastingPromptViewDelegate) {
        // 错误点:协议类型没有removeFromSuperview方法,该方法属于UIView
        sender.removeFromSuperview()
        self.backgroundView.removeFromSuperview()
    }
}

错误原因

两个报错都由代理方法参数类型定义失配导致:

  • 协议中将dismissAlert的参数声明为CastingPromptViewDelegate协议类型,但实际调用时传入的是CastingPromptView视图实例,类型不匹配触发第二个报错
  • 代理回调实现中,试图对协议类型的参数调用仅UIView类具备的removeFromSuperview方法,触发第一个报错

修复方案

只需要修正代理方法的参数类型,让语义和实际传值匹配即可:

  1. 修改协议定义,将参数类型从协议类型改为实际的视图类型CastingPromptView:
protocol CastingPromptViewDelegate {
    func dismissAlert(sender: CastingPromptView)
}
  1. 同步修改WatchVC中代理方法的实现,参数类型对齐:
extension WatchVC:CastingPromptViewDelegate {
    func dismissAlert(sender: CastingPromptView) {
        sender.removeFromSuperview()
        self.backgroundView.removeFromSuperview()
    }
}

修改后类型完全匹配,两个编译错误会直接消失。


内容的提问来源于stack exchange,提问作者NinjaDeveloper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.03 01:19:07