0
0
Swiftprogramming~10 mins

Protocol extensions for shared behavior 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 protocol named 'Greetable'.

Swift
protocol [1] {
    func greet() -> String
}
Drag options to blanks, or click blank then click option'
AGreet
BGreeter
CGreeting
DGreetable
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class or struct name instead of a protocol name.
2fill in blank
medium

Complete the protocol extension to provide a default implementation for 'greet()'.

Swift
extension Greetable {
    func greet() -> String {
        return [1]
    }
}
Drag options to blanks, or click blank then click option'
A"Hello!"
BHello
Cgreet()
Dprint("Hello!")
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a function call or printing instead of returning a string.
3fill in blank
hard

Fix the error by completing the struct declaration that conforms to 'Greetable'.

Swift
struct Person: [1] {
    var name: String
}
Drag options to blanks, or click blank then click option'
AGreeting
BGreeter
CGreetable
DGreet
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect protocol names or forgetting conformance syntax.
4fill in blank
hard

Fill both blanks to override the default 'greet()' method in the struct.

Swift
extension Person {
    func greet() -> String {
        return [1] + ", " + [2]
    }
}
Drag options to blanks, or click blank then click option'
A"Hi"
Bname
C"Hello"
Dgreet()
Attempts:
3 left
💡 Hint
Common Mistakes
Using method calls instead of string or property names.
5fill in blank
hard

Fill all three blanks to create a dictionary that maps names to greetings for a list of people.

Swift
let greetings = Dictionary(uniqueKeysWithValues: people.filter({ person in person.name.count [2] 3 && person.greet() [3] "Hello!" }).map({ person in (person.name, person.[1]()) }))
Drag options to blanks, or click blank then click option'
Agreet
B>
C!=
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using property 'name' instead of method 'greet' for greetings.
Using wrong comparison operators.