0
0
iOS Swiftmobile~20 mins

Dependency injection in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dependency Injection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
What is the main benefit of dependency injection in Swift?

Choose the best explanation for why developers use dependency injection in Swift apps.

AIt allows objects to create their own dependencies internally for better encapsulation.
BIt automatically manages memory for all dependencies without manual code.
CIt helps to reduce tight coupling by providing dependencies from outside the object.
DIt forces all dependencies to be global singletons for easy access.
Attempts:
2 left
💡 Hint

Think about how dependency injection affects how objects get what they need to work.

component_behavior
intermediate
1:30remaining
What will be printed when this Swift code runs?

Consider this Swift code using dependency injection. What is the output?

iOS Swift
protocol Logger {
    func log(_ message: String)
}

class ConsoleLogger: Logger {
    func log(_ message: String) {
        print("Console: \(message)")
    }
}

class FileLogger: Logger {
    func log(_ message: String) {
        print("File: \(message)")
    }
}

class Service {
    let logger: Logger
    init(logger: Logger) {
        self.logger = logger
    }
    func doWork() {
        logger.log("Work done")
    }
}

let service = Service(logger: FileLogger())
service.doWork()
ANo output
BFile: Work done
CWork done
DConsole: Work done
Attempts:
2 left
💡 Hint

Look at which logger instance is passed to the Service.

📝 Syntax
advanced
2:00remaining
Which option correctly injects a dependency using initializer injection in Swift?

Choose the correct Swift code snippet that injects a dependency via the initializer.

A
class ViewController {
    var service: Service
    init(service: Service) {
        self.service = service
    }
}
B
class ViewController {
    var service: Service
    init() {
        service = Service()
    }
}
C
class ViewController {
    var service = Service()
    init(service: Service) {
        self.service = service
    }
}
D
class ViewController {
    var service: Service
    func inject(service: Service) {
        self.service = service
    }
}
Attempts:
2 left
💡 Hint

Initializer injection requires the dependency to be passed and assigned in the init method.

🔧 Debug
advanced
2:00remaining
Why does this Swift code cause a runtime crash?

Examine the code below. What causes the crash when creating ViewController?

iOS Swift
class Service {}

class ViewController {
    var service: Service!
    init() {
        // service is not initialized here
    }
    func useService() {
        print(service!)
    }
}

let vc = ViewController()
vc.useService()
AThe useService method is called before the ViewController instance is created.
BThe init method is missing a call to super.init(), causing a crash.
CThe service property is a constant and cannot be assigned later.
DThe service property is declared as implicitly unwrapped optional but never initialized before use.
Attempts:
2 left
💡 Hint

Check how the service property is declared and when it is assigned.

state_output
expert
2:30remaining
What is the final value of counter after running this Swift code with dependency injection?

Analyze the code below. What value does counter have after incrementer.increment() is called?

iOS Swift
protocol Incrementable {
    func increment()
}

class Counter: Incrementable {
    var count = 0
    func increment() {
        count += 1
    }
}

class Incrementer {
    let counter: Counter
    init(counter: Counter) {
        self.counter = counter
    }
    func increment() {
        counter.increment()
    }
}

let counter = Counter()
let incrementer = Incrementer(counter: counter)
incrementer.increment()
let finalCount = counter.count
A1
B0
C2
DCompilation error
Attempts:
2 left
💡 Hint

Trace how the count property changes when increment() is called.