Hi all, I'm facing a problem that I came up with the following code (simplified for illustration purposes):
typealias Handler = (String) -> Void
// class B scope
var handlerSaver: Handler?
// saves the closure parameter (handler) to be executed later
classA.foo: ((Handler) -> Void) = { handler in
// error: Assigning non-escaping parameter 'handler' to an @escaping closure
handlerSaver = handler
}
// got stuff ...
handlerSaver("stuff")
// class A scope
var foo: ((Handler) -> Void)?
var handler = { (string: String) -> Void in
print("handle this \(string)")
}
// execute foo's closure with another closure (handler) as argument
foo?(handler)
Can this error be fixed? In other words I would like that class A calls foo but its handler being called later by class B. Is there any other way to achieve this instead of implementing a func handler(_ string: String)
in class A; so the class B would call for it like so classA.handler("stuff")
?
Thanks!