0
0
Swiftprogramming~5 mins

Sendable protocol for thread safety in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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
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
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
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
Which keyword is used to declare a type conforms to Sendable?
ASendable
Bthreadsafe
Csendable
Dconcurrent
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.