What is the purpose of the Sendable protocol in Swift?
The Sendable protocol marks types as safe to be used across different threads without causing data races or undefined behavior.
Click to reveal answer
intermediate
How does Swift check if a type conforms to Sendable?
Swift uses compile-time checks to ensure that all stored properties of a type are also Sendable, making the whole type safe for concurrent use.
Click to reveal answer
intermediate
Can classes conform to Sendable automatically? Why or why not?
No, classes do not automatically conform because they have reference semantics and can cause data races. You must manually ensure thread safety and mark them as Sendable if safe.
Click to reveal answer
beginner
What keyword do you use to declare a type as conforming to Sendable?
You use the Sendable protocol in the type declaration, for example: struct MyType: Sendable { ... }.
Click to reveal answer
beginner
Why is Sendable important in Swift concurrency?
Sendable helps the compiler ensure that data passed between concurrent tasks is safe, preventing bugs caused by simultaneous access to mutable state.
Click to reveal answer
What does the Sendable protocol guarantee in Swift?
AThat a type is immutable
BThat a type can be safely shared across threads
CThat a type is a class
DThat a type is only used on the main thread
✗ Incorrect
Sendable ensures a type is safe to use across threads, not necessarily immutable or restricted to the main thread.
Which of these types automatically conforms to Sendable in Swift?
AStructs with only <code>Sendable</code> properties
BAll classes
CFunctions
DEnums with associated reference types
✗ Incorrect
Structs with only Sendable properties automatically conform. Classes and enums with reference types do not.
If a class has mutable state, can it conform to Sendable safely without extra work?
AOnly if it inherits from NSObject
BYes, always
CNo, you must ensure thread safety manually
DOnly if it is final
✗ Incorrect
Mutable state in classes requires manual thread safety measures before conforming to Sendable.
What happens if you try to pass a non-Sendable type between concurrent tasks?
AThe program crashes immediately
BNothing, it works fine
CThe type is automatically copied
DThe compiler will produce an error or warning
✗ Incorrect
Swift's compiler checks and warns or errors if non-Sendable types are passed between concurrent contexts.
Which keyword is used to declare a type conforms to Sendable?
ASendable
Bthreadsafe
Csendable
Dconcurrent
✗ Incorrect
The protocol name Sendable is used exactly as is in the type declaration.
Explain what the Sendable protocol is and why it matters for thread safety in Swift.
Think about how Swift prevents data races when sharing data between threads.
You got /4 concepts.
Describe how you would make a custom struct conform to Sendable safely.
Consider what properties your struct has and their thread safety.
You got /4 concepts.
Practice
(1/5)
1. What does the Sendable protocol in Swift ensure?
easy
A. That a function runs asynchronously.
B. That a class can only be used on the main thread.
C. That a variable is immutable.
D. That data can be safely shared between threads without causing race conditions.
Solution
Step 1: Understand the purpose of Sendable
The Sendable protocol marks types that can be safely passed between threads without causing data races.
Step 2: Compare options with the definition
Only That data can be safely shared between threads without causing race conditions. correctly describes this safety guarantee. Other options describe unrelated concepts.
Final Answer:
That data can be safely shared between threads without causing race conditions. -> Option D
Quick Check:
Sendable = safe cross-thread data sharing [OK]
Hint: Sendable means safe to share data across threads [OK]
Common Mistakes:
Confusing Sendable with immutability
Thinking Sendable restricts to main thread only
Assuming Sendable makes functions async
2. Which of the following is the correct way to declare a struct as Sendable in Swift?
easy
A. struct Data { var value: Int } implements Sendable
B. struct Data: Sendable { var value: Int }
C. struct Data Sendable { var value: Int }
D. struct Data { var value: Int } : Sendable
Solution
Step 1: Recall Swift protocol conformance syntax
In Swift, a type conforms to a protocol by listing it after a colon following the type name.
Step 2: Match syntax to options
struct Data: Sendable { var value: Int } uses correct syntax: struct Data: Sendable { ... }. Others are invalid Swift syntax.
Final Answer:
struct Data: Sendable { var value: Int } -> Option B
Quick Check:
Protocol conformance uses colon after type name [OK]
Hint: Use colon after struct name to conform to Sendable [OK]
Common Mistakes:
Placing protocol after braces
Using 'implements' keyword (not Swift)
Omitting colon between type and protocol
3. What will happen when you try to compile this Swift code?
struct Counter: Sendable {
var count: Int
}
let counter = Counter(count: 5)
Task {
print(counter.count)
}
medium
A. It compiles and prints 5.
B. It causes a compile-time error because Counter is not Sendable.
C. It causes a runtime error due to thread safety violation.
D. It compiles but prints 0.
Solution
Step 1: Check if Counter conforms to Sendable
The struct explicitly conforms to Sendable and contains only an Int, which is Sendable.
Step 2: Understand Task usage
The Task runs asynchronously but can safely access counter because it is Sendable.
Final Answer:
It compiles and prints 5. -> Option A
Quick Check:
Sendable struct with Int prints value safely [OK]
Hint: Sendable structs with simple types compile and run safely [OK]
Common Mistakes:
Assuming compile error without checking conformance
Confusing runtime errors with compile errors
Thinking default structs are not Sendable
4. Identify the error in this Swift code using Sendable:
class DataHolder: Sendable {
var data: [Int]
init(data: [Int]) {
self.data = data
}
}
medium
A. Missing explicit Sendable conformance on Array.
B. Class cannot conform to Sendable at all.
C. Array<Int> is not Sendable by default, causing a compile error.
D. No error; code compiles fine.
Solution
Step 1: Check Sendable conformance for class with mutable property
Classes with mutable stored properties like arrays are not automatically Sendable.
Step 2: Understand why Array<Int> is not Sendable by default
Array is a reference type and mutable, so compiler disallows automatic Sendable conformance without extra guarantees.
Final Answer:
Array<Int> is not Sendable by default, causing a compile error. -> Option C