Recall & Review
beginner
What is dependency injection in simple terms?
Dependency injection means giving an object what it needs instead of letting it create those things itself. It helps keep code clean and easy to change.
Click to reveal answer
beginner
Why is dependency injection useful in iOS Swift apps?
It makes testing easier because you can swap real parts with fake ones. It also helps organize code so parts don’t depend too much on each other.
Click to reveal answer
intermediate
What are the common ways to do dependency injection in Swift?
You can inject dependencies through initializer methods, by setting properties after creating an object, or by using a dependency container that provides needed parts.
Click to reveal answer
beginner
Show a simple Swift example of initializer injection.
```swift
protocol Service {
func doWork()
}
class RealService: Service {
func doWork() { print("Working") }
}
class Client {
let service: Service
init(service: Service) {
self.service = service
}
func start() {
service.doWork()
}
}
let service = RealService()
let client = Client(service: service)
client.start() // prints "Working"
```Click to reveal answer
intermediate
What problem does dependency injection solve compared to creating dependencies inside a class?
It avoids tight coupling, so classes don’t depend on specific implementations. This makes code easier to change, test, and reuse.
Click to reveal answer
What is the main goal of dependency injection?
✗ Incorrect
Dependency injection means providing dependencies from outside, not creating them inside.
Which of these is NOT a common way to inject dependencies in Swift?
✗ Incorrect
Inheritance injection is not a standard term or method for dependency injection.
Why does dependency injection help with testing?
✗ Incorrect
Dependency injection lets you swap real dependencies with mocks or fakes for testing.
In the example: `init(service: Service)`, what is happening?
✗ Incorrect
The service is passed into the class through the initializer, which is initializer injection.
What does tight coupling mean in code?
✗ Incorrect
Tight coupling means classes rely heavily on specific details, making changes harder.
Explain dependency injection and why it is helpful in iOS Swift development.
Think about how giving objects what they need from outside helps keep code clean.
You got /3 concepts.
Describe how you would use initializer injection in a Swift class with a simple example.
Remember the example with a service protocol and a client class.
You got /3 concepts.