0
0
Swiftprogramming~20 mins

Sendable protocol for thread safety in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Sendable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Swift code using Sendable?

Consider this Swift code snippet using the Sendable protocol. What will be printed?

Swift
import Foundation

struct Counter: Sendable {
    var value: Int
    mutating func increment() {
        value += 1
    }
}

func updateCounter(counter: inout Counter) {
    counter.increment()
}

var myCounter = Counter(value: 0)
updateCounter(counter: &myCounter)
print(myCounter.value)
A1
B0
CCompilation error due to Sendable conformance
DRuntime error due to thread safety violation
Attempts:
2 left
💡 Hint

Think about what Sendable means for value types and how mutation affects the value.

🧠 Conceptual
intermediate
1:30remaining
Which type automatically conforms to Sendable in Swift?

Which of the following Swift types automatically conform to the Sendable protocol without extra code?

AEnums with associated reference types
BClosures capturing mutable state
CClasses with mutable properties
DStructs with only stored properties that are Sendable
Attempts:
2 left
💡 Hint

Think about value types and their stored properties.

🔧 Debug
advanced
2:00remaining
What error does this code raise related to Sendable?

Examine this Swift code snippet. What error will the compiler produce?

Swift
import Foundation

class DataHolder {
    var data: String
    init(data: String) {
        self.data = data
    }
}

struct Container: Sendable {
    var holder: DataHolder
}
ANo error, code compiles successfully
BRuntime error due to data race
CType 'DataHolder' does not conform to 'Sendable'
DMissing initializer error
Attempts:
2 left
💡 Hint

Check if classes conform to Sendable automatically.

📝 Syntax
advanced
1:30remaining
Which option correctly declares a Sendable class in Swift?

Which of the following Swift class declarations correctly conforms to Sendable?

Aclass UnsafeClass: Sendable {}
Bfinal class SafeClass: Sendable {}
Cfinal class SafeClass: Sendable { var value: Int }
Dclass SafeClass: Sendable { let value: Int }
Attempts:
2 left
💡 Hint

Consider what makes a class conform to Sendable safely.

🚀 Application
expert
3:00remaining
How many items are in the dictionary after this concurrent Sendable code runs?

Given this Swift code using concurrency and Sendable, how many items will the dictionary contain after execution?

Swift
import Foundation

@Sendable
func addItem(to dict: inout [Int: String], key: Int, value: String) {
    dict[key] = value
}

var sharedDict = [Int: String]()

let queue = DispatchQueue(label: "concurrent", attributes: .concurrent)

for i in 0..<5 {
    queue.async(flags: .barrier) {
        addItem(to: &sharedDict, key: i, value: "Item \(i)")
    }
}

queue.sync(flags: .barrier) {}

print(sharedDict.count)
A5
B0
CLess than 5 due to race conditions
DCompilation error due to Sendable misuse
Attempts:
2 left
💡 Hint

Consider how the barrier flag affects concurrent writes.