Пример использования блока (замыкания) как свойства класса в Swift.
class ChatViewController: UIViewController {
private var someClosure: (() -> Void)?
private var anotherClosure: ((arg: Double) -> Bool)?
func executeClosures() {
if self.someClosure != nil {
self.someClosure()
}
if self.anotherClosure != nil {
let boolResult = self.anotherClosure(arg: 2.0)
}
}
func addSelfClosure(closure: (() -> Void)!) {
self.someClosure = closure
}
func printSomething() {
self.addSelfClosure() {
print("closure called")
}
self.executeClosures() // prints: closure called
}
}