Answer the following Swift/iOS fundamentals questions.
Assume the current thread is the main thread.
What is the output order? Which thread executes each print?
let queue = DispatchQueue(label: "test")
queue.async {
print(1)
}
queue.sync {
print(2)
}
print(3)
What is the output order and why?
var str = "1"
DispatchQueue.main.async {
str += "2"
print(str)
}
DispatchQueue.main.async {
str += "3"
print(str)
}
print(str)
Also explain why the second async block prints "123" rather than "13".
class
vs
struct
in Swift (semantics, performance, common use cases).
weak
vs
unowned
. Why can
unowned
crash while
weak
typically doesn’t?
Login required