I have a closure that accepts another closure:
var onSend: ((String, (Int) -> Void) -> Void)?
which is called like:
onSend?(messageText) { [weak self] messageID in
self?.markAsSent(messageID)
}
and assigned as:
vc.onSend = { [unowned messageService] message, callback in
messageService.send(message) { result in
switch result {
case let .success(messageID): callback(messageID)
case let .failure(error): fatalError("failed to send a message with error: \(error)")
}
}
}
But the snippet above fails to compile with the error:
Closure use of non-escaping parameter 'callback' may allow it to escape
I wonder if there is a way to mark the callback
in the previous snippet as @escaping
?
In case it matters, messageService.send/2
is defined as follows:
func send(_ message: String,
then callback: @escaping (Result<Int, MessageServiceError>) -> Void)
Or should I maybe try assigning vc.onSend
to a function?
vc.onSend = self.onSend
where self.onSend
is
private func onSend(message: String, @escaping callback: (Int) -> Void) {
messageService.send(message) { result in
switch result {
case let .success(messageID): callback(messageID)
case let .failure(error): fatalError("failed to send a message with error: \(error)")
}
}
}
But it forces me to assign messageService
somewhere so that it could be accessed from func onSend
...