Reputation: 20058
I am trying to run this simple example using a closure for learning purpose, but doesn't seem to work as I expected:
class Test {
var callback: ((_ value: Int) -> Void)?
func perform() {
callback
}
}
let t = Test()
t.callback = { _ in
print("Test")
}
t.perform()
I was expected that "Test"
will get printed, but apparently it's not. Can someone point what the issue is ?
Upvotes: 0
Views: 99
Reputation: 539685
Compiling the code reveals the error:
error: expression resolves to an unused l-value callback ^~~~~~~~
callback
is just the (optional) closure, not a call to the closure.
Apparently the Playground does not complain about the unused expression.
Calling the closure with some argument fixes the problem:
func perform() {
callback?(5)
}
Upvotes: 1