将协议方法标记为已弃用

问题描述 投票:16回答:1

对于实现协议的人,如何使协议方法显示为已弃用?我已尝试使用如下所示的@available,但在实现协议方法时Xcode中没有显示警告。

protocol TestDelegate {
    @available(*, deprecated, message: "Don't use this anymore")
    func myMethod() -> Bool
}

extension ViewController: TestDelegate {
    func myMethod() -> Bool {
        return true
    }
}
swift swift-protocols deprecation-warning
1个回答
9
投票

信息

About attributes

细节

  • Xcode 10.2.1(10E1001),Swift 5

@objc
protocol TestDelegate {
    @available(iOS, unavailable)
    func myMethod1() -> Bool

    @available(iOS, unavailable, message: "Don't use this anymore")
    func myMethod2() -> Bool

    @available(iOS, unavailable, renamed: "myMethod4()")
    func myMethod3() -> Bool

    @available(iOS, obsoleted: 10.0)
    func myMethod4() -> Bool

    @available(swift, introduced: 3.0, obsoleted: 4.2)
    func myMethod5() -> Bool

    @available(iOS, introduced: 8.0, obsoleted: 11.0)
    func myMethod6() -> Bool
}

extension ViewController: TestDelegate {

    func myMethod1() -> Bool { return true }
    func myMethod2() -> Bool { return true }
    func myMethod3() -> Bool { return true }
    func myMethod4() -> Bool { return true }
    func myMethod5() -> Bool { return true }
    func myMethod6() -> Bool { return true }
}

校验

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.