0
0
Swiftprogramming~10 mins

Extension 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 an extension for the String type.

Swift
extension String [1] {
    func greet() {
        print("Hello, \(self)!")
    }
}
Drag options to blanks, or click blank then click option'
A(
B{
C[
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or brackets instead of curly braces for the extension body.
2fill in blank
medium

Complete the code to add a computed property 'isLong' to String that returns true if the string length is greater than 10.

Swift
extension String {
    var isLong: Bool [1] {
        return self.count > 10
    }
}
Drag options to blanks, or click blank then click option'
Aget()
Bset
Cget
Dset()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' instead of 'get' for a read-only computed property.
Adding parentheses after 'get'.
3fill in blank
hard

Fix the error in the extension by completing the method signature correctly.

Swift
extension Int {
    func squared() -> Int [1] {
        return self * self
    }
}
Drag options to blanks, or click blank then click option'
A{
B(
C[
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or brackets instead of curly braces after the method signature.
4fill in blank
hard

Fill both blanks to add a mutating method 'increment' to Int that increases its value by 1.

Swift
extension Int {
    mutating func increment() [1] [2] self += 1
}
Drag options to blanks, or click blank then click option'
A{
B}
C-> Void
Din
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting curly braces or using '-> Void' incorrectly.
Using 'in' which is for closures, not method bodies.
5fill in blank
hard

Fill all three blanks to add a static method 'description' to Double that returns a string describing the type.

Swift
extension Double {
    static func description() [1] String [2] {
        return "Double type"
    [3]
Drag options to blanks, or click blank then click option'
A->
B{
C}
Din
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the return arrow '->'.
Misplacing or missing curly braces.
Using 'in' which is for closures.