gpt4 book ai didi

swift - 快速实现完成 block

转载 作者:行者123 更新时间:2023-11-30 11:21:01 25 4
gpt4 key购买 nike

我已经实现了有逻辑错误的完成 block 。我希望当单击 checkOutBtn 时,首先触发 checkFields 以检查所有文本字段是否不为空,然后再触发addingDeliveryAddress() 方法插入数据库,然后再执行 sesueway。但它不像那样工作,当单击 checkOutBtn 时,它会继续执行 segueway。感谢你的帮助。谢谢

   @IBAction func checkOutBtn(_ sender: Any) {

checkFields { (results) in
if results {
self.addingDeliveryAddress()
}
}
}


func checkFields(_ completion: @escaping (Bool) -> ()){
if (recipientName.text?.isEmpty)! {
errorMessageLbl.textColor = UIColor.red
errorMessageLbl.text = "Enter Recipient Name"
completion(false)
}else if (recipientMobile.text?.isEmpty)! {
errorMessageLbl.textColor = UIColor.red
errorMessageLbl.text = "Enter Recipient Mobile Number"
completion(false)
}else if (recipientArea.text?.isEmpty)! {
errorMessageLbl.textColor = UIColor.red
errorMessageLbl.text = "Enter Recipient Area"
completion(false)
}else if (recipientAddress.text?.isEmpty)! {
errorMessageLbl.textColor = UIColor.red
errorMessageLbl.text = "Enter Recipient Address"
completion(false)
}
completion(true)
}



//Adding Delivery Address
func addingDeliveryAddress(){

//getting user data from defaults
let defaultValues = UserDefaults.standard
let userId = defaultValues.string(forKey: "userid")

//creating parameters for the post request
let parameters: Parameters=[
"recipientName":recipientName.text!,
"recipientPhoneNumber":recipientMobile.text!,
"recipientArea":recipientArea.text!,
"recipientAddress":recipientAddress.text!,
"nearestLandmark":recipientLandmark.text!,
"userId":Int(userId!)!
]

//Constant that holds the URL for web service
let URL_ADD_DELIVERY_ADDRESS = "http://localhost:8888/restaurant/addDeliveryAddress.php?"

Alamofire.request(URL_ADD_DELIVERY_ADDRESS, method: .post, parameters: parameters).responseJSON {
response in
//printing response
print(response)

let result = response.result.value

//converting it as NSDictionary
let jsonData = result as! NSDictionary

//if there is no error
if(!(jsonData.value(forKey: "error") as! Bool)){

self.performSegue(withIdentifier: "toCheckOut", sender: self)

}else{

let alert = UIAlertController(title: "No Delivery Address", message: "Enter Delivery Address to continue", preferredStyle: .alert)

alert.addAction(UIAlertAction(title: "Ok", style: .destructive, handler: nil))
//alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))

self.present(alert, animated: true)
}
}
}

最佳答案

为什么要完成 block ?没有异步过程。

我建议这种方式(直接)返回错误字符串或成功时返回空字符串。

@IBAction func checkOutBtn(_ sender: Any) {

let result = checkFields()
if result.isEmpty {
self.addingDeliveryAddress()
} else {
errorMessageLbl.textColor = UIColor.red
errorMessageLbl.text = "Enter Recipient " + result
}
}

func checkFields() -> String {
if recipientName.text!.isEmpty {
return "Name"
} else if recipientMobile.text!.isEmpty {
return "Mobile Number"
} else if recipientArea.text!.isEmpty {
return "Area"
} else if recipientAddress.text!.isEmpty {
return "Address"
}
return ""
}

关于swift - 快速实现完成 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51269782/

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