0
0
Swiftprogramming~10 mins

Opaque types with some keyword 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 function that returns some View.

Swift
func makeView() -> [1] {
    Text("Hello")
}
Drag options to blanks, or click blank then click option'
AView
Bsome View
Cany View
DOpaque View
Attempts:
3 left
💡 Hint
Common Mistakes
Using just the protocol name without 'some' causes errors.
Using 'any' instead of 'some' changes the meaning.
2fill in blank
medium

Complete the code to declare a variable with an opaque type conforming to Equatable.

Swift
var value: [1] = 5
Drag options to blanks, or click blank then click option'
Asome Equatable
Bany Equatable
CEquatable
DOpaque Equatable
Attempts:
3 left
💡 Hint
Common Mistakes
Using just the protocol name without 'some' is not allowed for variables.
Using 'any' instead of 'some' changes the meaning to an existential type.
3fill in blank
hard

Fix the error in the function return type to use opaque types correctly.

Swift
func getNumber() -> [1] {
    return 42
}
Drag options to blanks, or click blank then click option'
AInt
Bsome Int
CNumeric
Dsome Numeric
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a concrete type without 'some' when an opaque type is expected.
Using 'some Int' is invalid because Int is a concrete type, not a protocol.
4fill in blank
hard

Fill both blanks to create a function returning an opaque type conforming to CustomStringConvertible.

Swift
func describe() -> [1] {
    return [2](value: 10)
}

struct Wrapper: CustomStringConvertible {
    var value: Int
    var description: String { "Value is \(value)" }
}
Drag options to blanks, or click blank then click option'
Asome CustomStringConvertible
BWrapper
CCustomStringConvertible
Dsome Wrapper
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the protocol type directly instead of an opaque type.
Returning a type that does not conform to the protocol.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension with opaque types and a condition.

Swift
let filteredLengths = ["apple", "banana", "cherry"].reduce(into: [:]) { dict, word in
    if word.count [1] 5 {
        dict[word] = word.count [2] 2
    }
}

func makeOpaque() -> [3] {
    return filteredLengths
}
Drag options to blanks, or click blank then click option'
A>
B*
Csome Collection
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators causing logic errors.
Returning a concrete type without 'some' keyword.