iOS Interview Question (Part 4)
1. Can we use let property with protocol?
No, it is not possible to use a let
property with a protocol in Swift.
Properties declared in a protocol are by default read-write, and you cannot specify a let
property in a protocol.
4. Diff b/w Tight Coupling vs Loose Coupling
Tight Coupling means one class is dependent on another class.
Loose Coupling means one class is dependent on interface rather than class.
5. Diff b/w dependency inversion principle vs dependency injection in ios
- Dependency Inversion Principle (DIP) focuses on the design principle that encourages using abstractions (interfaces or protocols) to depend on, rather than concrete implementations. It helps in achieving loose coupling and modular design.
- Dependency Injection (DI) is a pattern that implements DIP by providing dependencies to a class from the outside. In iOS, this can be achieved through constructor injection, property injection, or method injection. DI helps in managing and providing dependencies to classes, making them more flexible and testable.
In summary, DIP is a design principle that promotes depending on abstractions, while DI is a pattern that implements DIP by externalizing the dependency management and providing dependencies to classes from the outside. By applying DIP and DI principles, you can create more modular, flexible, and testable iOS applications.
6. What is the difference between while and repeat-while loops in swift?
In Swift, the while
and repeat-while
loops are used for repetitive execution of code blocks, but they differ in when they evaluate their condition. Here's how they differ:
while
Loop: Thewhile
loop evaluates its condition before executing the code block. If the condition evaluates totrue
, the code block is executed. If the condition is initiallyfalse
, the code block is skipped entirely.
while condition {
// Code block
}
Example:
var count = 0
while count < 5 {
print(count)
count += 1
}
Output:
0
1
2
3
4
In the example, the code block inside the while
loop is executed as long as the condition count < 5
is true. The condition is checked before each iteration.
repeat-while
Loop: Therepeat-while
loop evaluates its condition after executing the code block. This guarantees that the code block is executed at least once, regardless of the condition's initial value. If the condition evaluates totrue
, the loop continues to execute. If the condition isfalse
, the loop terminates.
repeat {
// Code block
} while condition
Example:
var count = 0
repeat {
print(count)
count += 1
} while count < 5
Output:
0
1
2
3
4
In the example, the code block inside the repeat-while
loop is executed at least once, and then the condition count < 5
is checked. If the condition is true, the loop continues executing. If the condition is false, the loop terminates.
In summary, the key difference between while
and repeat-while
loops is when the condition is evaluated. The while
loop evaluates the condition before executing the code block, while the repeat-while
loop evaluates the condition after executing the code block.