How to capture a variable's current value for a block

后端 未结 2 1601
無奈伤痛
無奈伤痛 2021-01-21 12:17

Is there a way to save the current value of a varible for later usage in a block?

For example, for this Playground code:

import UIKit
import XCPlayground         


        
2条回答
  •  终归单人心
    2021-01-21 12:59

    You can bind an arbitrary expression to a named value in a capture list, the expression is evaluated when the closure is created. In your case you would bind self.i:

    dispatch_after(dispatchTime, dispatch_get_main_queue(), { [i = self.i] in
        self.test(i)
    })
    

提交回复
热议问题