【问题标题】:Argument passed to call that takes no arguments getting in Swift 4在 Swift 4 中传递给不带参数的调用的参数
【发布时间】:2019-10-21 12:03:14
【问题描述】:

我正在快速学习一些东西。我使用的是 Swift 4 版本。

我正在尝试做一些方法覆盖的事情。

class cricket {
    func print() {
        print("Super class called")
    }
}

class tennis : cricket {
    override func print() {
        print("Sub class called")

    }
}

let circInstance = cricket()
circInstance.print()

let tennisInstance = tennis()
tennisInstance.print()

但是,如果我在我的 Xcode 操场上运行上面的程序,在打印语句时会出现以下错误。

error: MyPlayground.playground:508:15: error: argument passed to call that takes no arguments
        print("Super class called")
             ~^~~~~~~~~~~~~~~~~~~~~

error: MyPlayground.playground:514:15: error: argument passed to call that takes no arguments
        print("Sub class called")
             ~^~~~~~~~~~~~~~~~~~~

有什么建议吗?

【问题讨论】:

    标签: ios swift swift4 swift-playground


    【解决方案1】:

    您与本地方法的名称冲突。您必须使用限定名称:

    Swift.print("Super class called")
    

    否则,您将尝试调用不带参数的self.print()

    Swift. 这里是包含print 函数的Swift Standard Library 模块的名称。

    【讨论】:

    • 谢谢,Swift.print() 是最新版的 swift print 语句语法吗?
    • @AnilkumariOSReactNative 这将适用于自 1.0 以来的所有 Swift 版本
    • 知道了,谢谢
    • @AnilkumariOSReactNative 如果您没有创建冲突的print() 方法,则无需执行Swift.print("..."),只需执行print("...")。我建议您将方法名称更改为 printClass() 之类的名称以避免混淆。
    猜你喜欢
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多