gpt4 book ai didi

ios - 如何或可以在函数的参数中传递结构?

转载 作者:行者123 更新时间:2023-11-28 14:52:52 26 4
gpt4 key购买 nike

我想创建一个结构类型参数,以便我只能传递两个值作为参数,但我不想使用 Bool。请帮助我使用以下代码,或者如果我做错了什么请详细说明。刚刚开始编码。

struct Alert {
let alert = "Alert"
let info = "Information"
}

func information(message : String) {
AskConfirmation(type : Alert.info , title: "Information", message: message, completion: { (bool) in
self.dismiss(animated: true, completion: nil)
})
}

func alert(message : String) {
AskConfirmation(type : Alert.alert , title: "Alert", message: message, completion: { (bool) in
self.dismiss(animated: true, completion: nil)
})
}

func askConfirmation (type : Alert.Type, title : String, message : String, completion:@escaping (_ result:Bool) -> Void) {
if type == Alert.alert {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
self.present(alert, animated: true, completion: nil)

alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { action in
completion(true)
}))

alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { action in
completion(false)
}))
}else if type == Alert.info {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
self.present(alert, animated: true, completion: nil)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { action in
completion(true)
}))
}
}

这就是我将如何使用我的自定义警报功能。

alert(message: "Priority not selected")
askConfirmation(type : "Information" , title: "Information", message: "Quote saved successfully", completion: nil)

最佳答案

使用enumeration相反:

enum Alert {
case alert
case info
}

func information(message : String) {
AskConfirmation(type : Alert.info , title: "Information", message: message, completion: { (bool) in
self.dismiss(animated: true, completion: nil)
})
}

func alert(message : String) {
AskConfirmation(type : Alert.alert , title: "Alert", message: message, completion: { (bool) in
self.dismiss(animated: true, completion: nil)
})
}

func askConfirmation (type : Alert, title : String, message : String, completion:@escaping (_ result:Bool) -> Void) {
if type == Alert.alert {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
self.present(alert, animated: true, completion: nil)

alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { action in
completion(true)
}))

alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { action in
completion(false)
}))
}else if type == Alert.info {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
self.present(alert, animated: true, completion: nil)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { action in
completion(true)
}))
}
}

在您的情况下,您希望拥有两个预定义的自定义值(因此不是 bool 值),枚举可以让您做到这一点。

我建议查看这些 advanced techniques也有枚举。

关于ios - 如何或可以在函数的参数中传递结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49669925/

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