Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 重构代码以显示alertbox(正确的方法是什么?)_Swift_Refactoring - Fatal编程技术网

Swift 重构代码以显示alertbox(正确的方法是什么?)

Swift 重构代码以显示alertbox(正确的方法是什么?),swift,refactoring,Swift,Refactoring,我正在处理iOS项目,我注意到一件事,每当我需要显示错误时,我会一次又一次地创建一个alertbox。我想重构代码以消除冗余。我的问题是:创建错误处理类是否是针对这种特定场景进行重构的适当方法?例如,我将创建以下类 class ErrorHandler { func ShowAlertBox(Title: String, Message: String, ViewController: UIViewController) { let alertControlle

我正在处理iOS项目,我注意到一件事,每当我需要显示错误时,我会一次又一次地创建一个alertbox。我想重构代码以消除冗余。我的问题是:创建错误处理类是否是针对这种特定场景进行重构的适当方法?例如,我将创建以下类

class ErrorHandler {

     func ShowAlertBox(Title: String, Message: String, ViewController: UIViewController) {

          let alertController = UIAlertController(title: Title, message: Message), preferredStyle: .Alert)
          let doneAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: "Label to cancel sign-in failure."), style: .Cancel, handler: nil)
    alertController.addAction(doneAction)
          ViewController.presentViewController(alertController, animated: true, completion: nil)
     }
}
这样称呼:

instanceofErrorHandler.ShowAlertBox("Error","Log In Error", SignInViewController)

我怀疑对于如何处理这种情况有很多不同的意见,我认为你的方法没有任何问题。也就是说,我这样做的方式是创建一个名为
ViewControllerUtilities
的扩展,并将所有常用函数放在其中:

protocol ViewControllerUtilities {}

extension ViewControllerUtilities where Self: UIViewController {
    func showAlert(_ title: String = "Error", message: String) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
        present(alertController, animated: true, completion: nil)
    }
}
仅供参考,我也有功能在那里检查网络是否可达。然后,我只需将
ViewControllerUtilities
添加到我的视图控制器所遵循的协议列表中,即可获得所有这些功能:

class MyViewController: UIViewController, ViewControllerUtilities {

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        showAlert("Error", message: "Sorry,  had an error.")
    }

}

我建议使用camelCase来标记类和变量之间的差异。但请学习使用标准命名约定。变量、属性和方法名称都应以小写字母开头。类名以大写字母开头。以下标准使您的代码更易于阅读(并且它将在堆栈溢出时正确着色)。@Emptyless OP正在使用camelcase。问题是变量名和方法名的开头都是大写字母。除此之外,您的代码还可以。@rmaddy CamelCase(也称为PascalCase)和CamelCase之间有区别——除此之外,我同意rmaddy。你的代码很好,我认为这是个好主意。但是有一个问题……我从来没有见过“where Self:UIViewController”的用法。从您的示例中,我假设这意味着ViewControllerUtilities继承自UIViewController类。正确吗?我假设“uTitle:String”是打字错误?@user30646不,
\uTitle:String
不是打字错误。@rmaddy:它做什么?下划线只允许您省略参数名。@user30646只是解释了
where Self:UIViewController
的用法。基本上,添加
where
条件使
ViewControllerUtilities
成为一个opt-in扩展,而不是将其应用于erey
UIVewController
对象