gpt4 book ai didi

ios - 什么是通过 Enum 进行可重用 UIAlertController 配置的 Swift 最佳实践

转载 作者:搜寻专家 更新时间:2023-11-01 06:03:22 31 4
gpt4 key购买 nike

我正在努力让我的生活更轻松一些,让我的应用程序更易于维护,同时减少重复代码的数量。所以我认为将一些代码用于显示特定类型的 UIAlertController 到它自己的类中会很好。

问题是,我有基本相同的警报,只是根据我在我的应用程序中显示它的位置而略有不同。所以我心想:为什么不使用枚举,每次我想显示那种警报时,只需给它枚举值。摆脱我代码中所有重复的字符串。

我喜欢 Swift 中的枚举。他们太狡猾了——嗯,我是说敏捷。所以我想出了这个枚举示例:

enum MyAlertType {
case a
case b
}

接下来我创建了一个简单的类来处理显示:

class MyAlert {
static func showAlert(ofType type: MyAlertType, inViewController viewController: UIViewController, handler: ((UIAlertAction) -> ())? = nil, completion: (() -> ())? = nil) {

var message: String
switch type {
case .a:
message = "A is a nice letter!"
case .b:
message = "B is a nice letter!"
}

let alert = UIAlertController(title: "Do you know which letter is nice?", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: handler))
viewController.present(alert, animated: true, completion: completion)
}
}

//Somewhere in my code (obviously a UIViewController)
MyAlert.showAlert(ofType: .a), inViewController: self)

但是等等,我仍然需要为这个函数提供我想在其中显示警报的 ViewController。对我来说,这总是相同的( self )所以下一个合乎逻辑的步骤是使它成为一个扩展:

extension UIViewController {
func showAlert(ofType type: MyAlertType, handler: ((UIAlertAction) -> ())? = nil, completion: (() -> ())? = nil) {

var message: String
switch type {
case .a:
message = "A is a nice letter!"
case .b:
message = "B is a nice letter!"
}

let alert = UIAlertController(title: "Do you know which letter is nice?", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: handler))
present(alert, animated: true, completion: completion)
}
}

//Somewhere in my UIViewController)
showAlert(ofType: .a)

但这使得所有 UIViewController 都可以使用代码,即使是那些我不明确需要/不想显示那种警报的 Controller 。我的意思是,是的,当然,我是开发人员,我可以决定不使用它,但另一方面,尽可能多地隐藏所有内容并不总是一个好习惯吗?谁知道将来谁会加入我的开发团队并开始以我未曾想到的方式滥用我的漂亮代码?

前几天我了解了 Swifts 面向协议(protocol)的编程方法(老实说我现在还没有完全理解)现在我想,我也许应该把它做成一个带有默认实现的协议(protocol)然后让唯一的那些UIViewControllers 实现协议(protocol),我需要在其中显示此警报。

protocol MyAlertProtocol {
func showAlert(ofType type: MyAlertType, handler: ((UIAlertAction) -> ())?, completion: (() -> ())?)
}

extension MyAlertProtocol where Self : UIViewController {
func showAlert(ofType type: MyAlertType, handler: ((UIAlertAction) -> ())? = nil, completion: (() -> ())? = nil) {

var message: String
switch type {
case .a:
message = "A is a nice letter!"
case .b:
message = "B is a nice letter!"
}

let alert = UIAlertController(title: "Do you know which letter is nice?", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: handler))
present(alert, animated: true, completion: completion)
}
}

extension MyViewController: MyAlertProtocol {}

我知道这听起来像是一个基于意见的问题,所以我不是问你是否认为这个或那个更好,而是告诉我是否确实 此场景的最佳实践及其外观。

通过协议(protocol)扩展默认实现的协议(protocol)?简单的 UIViewController 扩展?具有静态函数的自定义枚举、结构或类(如果是,是哪一个)?或者甚至可能只是 Swift 文件中某处的一个函数?我感到不知所措。选择的痛苦...

更新/解决方案

看完给出的答案后,我认为我的谢尔盖答案确实是最合适的。我想通过“减少重复代码行”来使我的喜欢“更容易”。对我来说,这包括我的 ViewController 中的“presend(controller:animated:)”。

但是,我认为你们是对的。我应该使用一个结构(一个静态函数实际上不需要一个类)和一个静态函数来生成 alert man 使它“准备好使用”但仍然让调用者决定在哪里呈现它。

通过这样做,我可以在任何我想要的地方使用我的警报生成结构,例如让一个代表展示它或传递它直到我到达一个可以展示它的 UIViewController,如果我的调用者不是一个.

因此在我非常简单的情况下,我会选择:

enum MyAlertType {
case a
case b
}

struct MyAlert {
static func showAlert(ofType type: MyAlertType, handler: ((UIAlertAction) -> ())? = nil) -> UIAlertController {

var message: String
switch type {
case .a:
message = "A is a nice letter!"
case .b:
message = "B is a nice letter!"
}

let alert = UIAlertController(title: "Do you know which letter is nice?", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: handler))

return alert
}
}

感谢所有参与解除我脑海中的封锁的人。

最佳答案

制作静态函数是一种常见且方便的方法。请注意,您创建的类型仅用于命名空间。

Alec O 的回答很好,但有时您想要在按下 OK 按钮时传递一些您想要执行的操作。此外,我会选择结构而不是类。

这是我的警报版本:

struct Alert {
static func errorAlert(title: String, message: String?, cancelButton: Bool = false, completion: (() -> Void)? = nil) -> UIAlertController {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let actionOK = UIAlertAction(title: "OK", style: .default) {
_ in
guard let completion = completion else { return }
completion()
}
alert.addAction(actionOK)

let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
if cancelButton { alert.addAction(cancel) }

return alert
}

ViewController 的子类中的用法:

    // Creating alerts:
let simpleAlert = Alert.errorAlert(title: "Error", message: "Simple message")
let alertWithCompletionAndCancel = Alert.errorAlert(title: "Message", message: "Message", cancelButton: true) {
// do something awesome
}

// Presenting alerts:
present(simpleAlert, animated: true)
present(alertWithCompletionAndCancel, animated: true)

关于ios - 什么是通过 Enum 进行可重用 UIAlertController 配置的 Swift 最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44030213/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com