【问题标题】:Issue between override and non-override function in swiftswift中覆盖和非覆盖函数之间的问题
【发布时间】:2015-06-25 13:21:12
【问题描述】:

抱歉,我不太擅长解释这些东西。基本上我有下面的函数来处理远程控制事件。

UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
override func remoteControlReceivedWithEvent(event: UIEvent) {
    if event.type == UIEventType.RemoteControl {
        if event.subtype == UIEventSubtype.RemoteControlPlay {
            stream.play()
        } else if event.subtype == UIEventSubtype.RemoteControlPause {
            stream.stop()
        } else if event.subtype == UIEventSubtype.RemoteControlTogglePlayPause {
            toggle()
        }
    }
}

基本上,当我使用“覆盖”一词(如上所示)时,我得到了错误

"方法不会覆盖其超类中的任何方法。

如果我省略“覆盖”,我会收到错误:

“带有Objective-C选择器'remoteControlReceivedWithEvent:'的方法'remoteControlReceivedWithEvent'与具有相同Objective-C选择器的超类“UIResponder”中的方法“remoteControlReceivedWithEvent”冲突。

我对这一切都很陌生,所以我真的不明白问题出在哪里。有人可以解释如何解决这个问题吗?如果您需要更多代码或其他内容,请告诉我。

我需要使用更多代码来设置它吗?

【问题讨论】:

    标签: xcode swift2


    【解决方案1】:

    UIResponder 方法签名与您的函数实现不匹配。 UIResponder 有如下可选事件: func remoteControlReceibedWithEvent(_ event: UIEvent?)

    所以它不能覆盖,因为没有带有非可选参数的函数,但是如果你删除覆盖它会与 ObjC 实现冲突,因为选择器名称是相同的。

    【讨论】:

    • 那么我该怎么做才能解决这个问题?
    • 添加 ?在 Event 之后的 func 中匹配 UIResponder 的签名。您还需要更改方法实现。
    • Np。我很乐意提供帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多