Articles, podcasts and news about Swift development, by John Sundell.

Calling initializers with dot syntax and passing them as closures

Published on 25 Mar 2017

Cool thing about Swift initializers: you can call them using dot syntax and pass them as closures! Perfect for mocking dates in tests.

class Logger {
    private let storage: LogStorage
    private let dateProvider: () -> Date
    init(storage: LogStorage = .init(), dateProvider: @escaping () -> Date = Date.init) { = storage
        self.dateProvider = dateProvider
    func log(event: Event) { event, date: dateProvider())