将协议方法标记为已弃用

时间:2016-11-01 21:48:58

标签: swift swift-protocols deprecation-warning

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

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

extension ViewController: TestDelegate {
    func myMethod() -> Bool {
        return true
    }
}

1 个答案:

答案 0 :(得分: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