変数を宣言してから、UIAlertAction 内でその値を設定しようとしています。「私はそこに行きます」ボタンをタップすると、以下のコードで出力は次のようになります。
1 thisCustomerRequesting: true
3 thisCustomerRequesting: false
「2 thisCustomerRequesting: ....」という行が出力にない理由と、「3 thisCustomerRequesting: false」が true であると期待しているのに false である理由を教えてください。
var thisCustomerRequesting = false
if thisCustomerRequesting == false {
let alert = UIAlertController(title: "title", message: "message", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "I am going there", style: .destructive, handler: {
(alertAction1: UIAlertAction) in
thisCustomerRequesting = true
print("1 thisCustomerRequesting: \(thisCustomerRequesting)")
}))
alert.addAction(UIAlertAction(title: "Close", style: .default,handler: nil))
self.present(alert, animated: true, completion: nil)
print("2 thisCustomerRequesting: \(thisCustomerRequesting)")
}
print("3 thisCustomerRequesting: \(thisCustomerRequesting)")