Daily iOS
Swift

Escaping closures

This code doesn't compile. What is the compiler asking for?

final class Loader {
    var handlers: [() -> Void] = []
    func register(_ handler: () -> Void) {
        handlers.append(handler)
    }
}
How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

Storing a closure in a property means it outlives the function call, so the compiler requires the parameter to be marked @escaping.

Share a question