Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a variable that can hold any type conforming to the protocol.
Swift
var value: [1] CustomStringConvertible Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'some' instead of 'any' which is for opaque types.
Using 'var' or 'let' inside the type declaration.
✗ Incorrect
The 'any' keyword is used to declare an existential type that can hold any value conforming to the protocol.
2fill in blank
mediumComplete the function signature to accept any type conforming to Equatable.
Swift
func compare(_ a: [1] Equatable, _ b: [1] Equatable) -> Bool
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'some' which is for opaque types and not allowed in parameter positions.
Omitting the keyword and causing a compile error.
✗ Incorrect
The 'any' keyword is used to accept any value conforming to the Equatable protocol as an existential type.
3fill in blank
hardFix the error in the code by adding the correct keyword for existential types.
Swift
let description: [1] CustomStringConvertible = "Hello"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'some' which is for opaque types and incompatible here.
Omitting the keyword causing a compiler error.
✗ Incorrect
The 'any' keyword is required to declare an existential type variable holding a value conforming to CustomStringConvertible.
4fill in blank
hardComplete the code to create a dictionary with keys as Strings and values as any type conforming to CustomStringConvertible.
Swift
let dict: [String: [1] CustomStringConvertible] = ["key": "value"]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'some' which is for opaque types and not allowed here.
Omitting 'any' from the type declaration causing a compiler error.
✗ Incorrect
The dictionary value type uses 'any' to indicate an existential type conforming to CustomStringConvertible. No keyword is needed before the string literal.
5fill in blank
hardFill both blanks to define a function returning any type conforming to Equatable and assign a value inside.
Swift
func getValue() -> [1] Equatable { let val: [2] Equatable = 42 return val }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'some' which is for opaque types and incompatible here.
Adding 'any' in the return statement which is not needed.
✗ Incorrect
The function return type and variable use 'any' for existential types. The return statement returns the variable directly without extra keywords.