Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a protocol named MyProtocol.
Swift
protocol [1] {
// protocol requirements
} Drag options to blanks, or click blank then click option'
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.✗ Incorrect
The keyword
protocol is followed by the protocol name, here MyProtocol.2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than the one required.
Omitting the parentheses after the method name.
✗ Incorrect
The protocol requires a method named
doSomething().3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding parentheses or braces incorrectly after the protocol name.
Missing the opening brace
{.✗ Incorrect
The protocol name must be followed by an opening brace
{ without extra characters.4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different protocol name than
Drawable.Spelling the property name as
colour instead of color.✗ Incorrect
The protocol name is
Drawable and the property required is named color.5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect protocol or method names.
Using a different parameter name than
size.✗ Incorrect
The protocol is named
Resizable, the method is resize, and the parameter name is size.