0

変数を宣言してから、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)")
4

1 に答える 1

1

出力 2 はコンソールにあるはずですが、 の上にあります1 thisCustomerRequesting: true

出力 3 は、ユーザーがボタンを押したときに非同期に設定される3 thisCustomerRequesting: falseためです。 その時点ですでに実行されています。thisCustomerRequestingtrue
print("3 thisCustomerRequesting: \(thisCustomerRequesting)")

于 2016-10-27T15:01:17.340 に答える