0
0
Swiftprogramming~10 mins

Sendable protocol for thread safety in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a struct as conforming to the Sendable protocol.

Swift
struct DataModel: [1] {
    var value: Int
}
Drag options to blanks, or click blank then click option'
AIdentifiable
BCodable
CSendable
DEquatable
Attempts:
3 left
💡 Hint
Common Mistakes
Using Codable instead of Sendable
Forgetting to conform to any protocol
2fill in blank
medium

Complete the code to mark a class as conforming to Sendable.

Swift
final class Counter: [1] {
    var count = 0
}
Drag options to blanks, or click blank then click option'
AObservableObject
BSendable
CIdentifiable
DCodable
Attempts:
3 left
💡 Hint
Common Mistakes
Using ObservableObject instead of Sendable
Not marking the class as final when conforming to Sendable
3fill in blank
hard

Fix the error by completing the code to make the struct Sendable when it contains a non-Sendable property.

Swift
struct Wrapper: Sendable {
    var data: [1]
}
Drag options to blanks, or click blank then click option'
AInt
BUnsafeMutablePointer<Int>
CString
DDispatchQueue
Attempts:
3 left
💡 Hint
Common Mistakes
Using pointer types which are not Sendable
Using DispatchQueue which is not Sendable
4fill in blank
hard

Fill both blanks to create a Sendable struct with a computed property.

Swift
struct Point: [1] {
    var x: Int
    var y: Int
    var description: [2] {
        "(\(x), \(y))"
    }
}
Drag options to blanks, or click blank then click option'
ASendable
BString
CInt
DCodable
Attempts:
3 left
💡 Hint
Common Mistakes
Using Codable instead of Sendable for the struct
Using Int instead of String for the description property
5fill in blank
hard

Fill all three blanks to create a Sendable struct with a closure property marked as @Sendable.

Swift
struct Task: [1] {
    var id: Int
    var action: [2] () -> Void
    init(id: Int, action: [3] () -> Void) {
        self.id = id
        self.action = action
    }
}
Drag options to blanks, or click blank then click option'
ASendable
B@Sendable
C() -> Void
DCodable
Attempts:
3 left
💡 Hint
Common Mistakes
Not marking the closure as @Sendable
Using Codable instead of Sendable for the struct