0
0
Swiftprogramming~10 mins

Existential types (any 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 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'
Aany
Bsome
Clet
Dvar
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'some' instead of 'any' which is for opaque types.
Using 'var' or 'let' inside the type declaration.
2fill in blank
medium

Complete 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'
Alet
Bsome
Cvar
Dany
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.
3fill in blank
hard

Fix 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'
Asome
Bvar
Cany
Dlet
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'some' which is for opaque types and incompatible here.
Omitting the keyword causing a compiler error.
4fill in blank
hard

Complete 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'
Aany
Bsome
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.
5fill in blank
hard

Fill 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'
Aany
Dsome
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.