我想创建一个结构类型参数,这样我只能传递两个值作为参数,但我不想使用Bool.Kindly帮我处理下面的代码,或者请详细说明我做错了什么。刚开始编码。
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)
答案 0 :(得分:1)
改为使用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)
}))
}
}
在您的情况下,您希望有两个预定义的自定义值(因此不是布尔值),枚举使您能够做到这一点。
我建议您使用枚举来查看这些advanced techniques。
答案 1 :(得分:0)
您的代码无法正常工作的原因是您正在直接访问尚未设置的值。
df[df.astype(str).KEY.str[1:3] == '02']
Out[34]:
KEY
1 1024876
将值更改为静态可让您直接访问它们。