簡體   English   中英

Swift 如何將編譯器警告添加到函數中

[英]Swift how to add compiler warning to function

我為 viewController 編寫了一個簡單的擴展,我想發出警告,以便其他開發人員不要直接調用此函數。 我不確定是否有一個屬性可以在 swift 中達到這個目的:

我的代碼:


internal extension UIViewController {

    // add warning so that it can appear when another developer wants to directly access this function
    func removeFromParent() {

        self.willMove(toParent: nil)
        self.view.removeFromSuperview()
        self.removeFromParent()
    }
}

因此,當另一個開發人員嘗試執行以下操作時:

let viewController = TestViewController()
viewController.removeFromParent()          

他們應該得到警告,你不能直接使用這個功能

在您的功能上方添加:

@available(*, deprecated, message: "use `someOtherFunction` instead")

您還可以添加這樣的評論:

/// DEPRECATED: you should use `someOtherFunction` instead

所以當你的開發者使用Alt + Click時它就會出現

添加警告->

您也可以使用#error("your message")

internal extension UIViewController {

    #warning("Your warning message")     
    func removeFromParent() {

        self.willMove(toParent: nil)
        self.view.removeFromSuperview()
        self.removeFromParent()
    }
}

函數調用時添加警告->

internal extension UIViewController {

    //@available(*, unavailable)

    //@available(*, deprecated, message: "your warning message")

    //@available(*, deprecated, renamed: "new name")     

    //@available(swift, introduced: 5)
    func removeFromParent() {

        self.willMove(toParent: nil)
        self.view.removeFromSuperview()
        self.removeFromParent()
    }
}

@available選項:

  • 不可用
  • 介紹
  • 已棄用
  • 過時的
  • 信息
  • 更名

如果您需要更多示例,請訪問hackingwithswift

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM