我有一个实用程序类,其中包含多个其他类使用的函数。其中之一是一个报警功能:默认情况下使用调用对象的“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
类。有什么方法可以重写这个以避免这种情况?