我有一個實用程序類,其中包含多個其他類使用的函數。其中之一是一個報警功能:默認情況下使用調用對象的「self」的方法
class Utils {
func doAlert (title: String, message: String, target: UIViewController) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Close", style: .cancel, handler: nil))
target.present(alert, animated: true, completion: nil)
}
}
此功能將始終瞄準視圖控制器上self
,所以我想不用我每次調用該函數時添加target: self
,但我不能只將其設置爲默認值,因爲這會導致它回到Utils
類。有什麼方法可以重寫這個以避免這種情況?