Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to make the struct conform to the protocol by adding the required property.
Swift
protocol Describable {
var description: String { get }
}
struct Car: Describable {
var make: String
var model: String
var description: String {
return [1]
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a print statement instead of a string.
Using the property name 'description' inside itself causing recursion.
✗ Incorrect
The description property must return a String. Using a string interpolation with make and model correctly returns the description.
2fill in blank
mediumComplete the extension to make Int conform to the protocol by implementing the required method.
Swift
protocol Squarable {
func squared() -> Int
}
extension Int: Squarable {
func squared() -> Int {
return [1]
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or subtraction instead of multiplication.
Dividing the number by itself which always returns 1.
✗ Incorrect
To return the square of the integer, multiply self by self.
3fill in blank
hardFix the error in the extension to correctly conform to the protocol by implementing the required computed property.
Swift
protocol Identifiable {
var id: String { get }
}
struct User {
var name: String
}
extension User: Identifiable {
var [1]: String {
return name
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different property name than the protocol requires.
Not implementing the property at all.
✗ Incorrect
The protocol requires a property named 'id'. The extension must implement 'id' to conform.
4fill in blank
hardFill both blanks to complete the extension that makes Array conform to the protocol by implementing the required method.
Swift
protocol Summable {
func sum() -> Int
}
extension Array: Summable where Element == Int {
func sum() -> Int {
return self.[1](0) { $0 [2] $1 }
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using * or - instead of + for summing.
Using map or filter instead of reduce.
✗ Incorrect
The reduce method with initial 0 and + operator sums all elements in the array.
5fill in blank
hardFill all three blanks to complete the extension that makes String conform to the protocol by implementing the required computed property and method.
Swift
protocol TextRepresentable {
var text: String { get }
func shout() -> String
}
extension String: TextRepresentable {
var [1]: String {
return self
}
func [2]() -> String {
return self.[3]()
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong property or method names.
Using 'lowercased()' instead of 'uppercased()' for shout.
✗ Incorrect
The property 'text' returns self. The method 'shout' returns the uppercase version using 'uppercased()'.