【问题标题】:Ios swift create a constructor for my classios swift为我的类创建一个构造函数
【发布时间】:2018-04-10 05:05:40
【问题描述】:

我是 swift 新手,正在使用 Swift 4.0 。我正在尝试为我的类创建一个构造函数,该构造函数接受 1 个字符串参数并且在使其工作时遇到问题。我也得到错误

调用中的参数标签不正确(具有“类型:”,预期为“编码器:”)

我一直在移动东西,但它不起作用。我想在下面更改我的子类,以便每当我想使用该类时,都需要以下签名 CustomAVPLayerC(type: "string") 。这是我第一次这样做,因为我通常使用函数。

这是我的代码

import UIKit
import AVKit

class CustomAVPLayerC: AVPlayerViewController {
    var ControllerType: String

   init(type: String) {
        self.ControllerType = type
        // perform some initialization here
       super.init(type: "")
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        if player?.isMuted == true {
            player?.isMuted = false
            //showsPlaybackControls = false

        } else {
            player?.isMuted = true

        }



    }

}

【问题讨论】:

  • 哪一行产生了错误?
  • 与您的问题无关,但变量名称应以小写字母开头。将ControllerType 更改为controllerType
  • 会改变变量名和super.init(type: "") 抛出错误
  • super.init(type: "")改成super.init(),这是来自BaseClass,你的基类没有super.init(type: "")

标签: ios swift


【解决方案1】:

您的init(type:) 初始化程序需要调用超类AVPlayerViewControllerinit 方法之一。 AVPlayerViewController 没有 init(type:) 初始化程序(存在于您的子类中)。

变化:

super.init(type: "")

到:

super.init()

【讨论】:

  • 哦,好吧,因为我的类是 AVPlayerViewController 类型,所以我会从那个类中找到一个 init。
  • 是的,这就是super 的意思——从基类调用方法。
猜你喜欢
  • 2021-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-30
  • 1970-01-01
  • 1970-01-01
  • 2020-02-02
相关资源
最近更新 更多