0
0
Swiftprogramming~10 mins

Protocol declaration syntax 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 MyProtocol.

Swift
protocol [1] {
    // protocol requirements
}
Drag options to blanks, or click blank then click option'
AMyProtocol
Bclass
Cfunc
Dstruct
Attempts:
3 left
💡 Hint
Common Mistakes
Using keywords like class or struct instead of the protocol name.
Forgetting to write the protocol name after protocol.
2fill in blank
medium

Complete the code to declare a protocol with a method requirement named doSomething().

Swift
protocol Actionable {
    func [1]()
}
Drag options to blanks, or click blank then click option'
AdoSomethingElse
BperformAction
CdoSomething
Dexecute
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than the one required.
Omitting the parentheses after the method name.
3fill in blank
hard

Fix the error in the protocol declaration by completing the code.

Swift
protocol [1] {
    func start()
}
Drag options to blanks, or click blank then click option'
AStartable
BStartable}
CStartable()
DStartable;
Attempts:
3 left
💡 Hint
Common Mistakes
Adding parentheses or braces incorrectly after the protocol name.
Missing the opening brace {.
4fill in blank
hard

Fill both blanks to declare a protocol named Drawable with a property requirement color of type String.

Swift
protocol [1] {
    var [2]: String { get set }
}
Drag options to blanks, or click blank then click option'
ADrawable
BDrawableProtocol
Ccolor
Dcolour
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different protocol name than Drawable.
Spelling the property name as colour instead of color.
5fill in blank
hard

Fill all three blanks to declare a protocol named Resizable with a method resize(to:) that takes a Double parameter named size.

Swift
protocol [1] {
    func [2](to [3]: Double)
}
Drag options to blanks, or click blank then click option'
AResizable
Bresize
Csize
Dscale
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect protocol or method names.
Using a different parameter name than size.