0
0
Swiftprogramming~10 mins

Adding methods 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 define a method named greet inside the struct.

Swift
struct Person {
    func [1]() {
        print("Hello!")
    }
}
Drag options to blanks, or click blank then click option'
Awelcome
Bgreet
Chello
DsayHello
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than 'greet'.
2fill in blank
medium

Complete the code to call the greet method on the person instance.

Swift
let person = Person()
person.[1]()
Drag options to blanks, or click blank then click option'
AsayHello
Bwelcome
Chello
Dgreet
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a method name that does not exist.
3fill in blank
hard

Fix the error by completing the method signature to accept a name parameter of type String.

Swift
struct Person {
    func greet([1] name: String) {
        print("Hello, \(name)!")
    }
}
Drag options to blanks, or click blank then click option'
Afor
Bto
Cwith
Dat
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect external parameter names like 'with' or 'to'.
4fill in blank
hard

Fill both blanks to define a method that returns a greeting string and call it.

Swift
struct Person {
    func greet() -> [1] {
        return "Hello!"
    }
}

let person = Person()
let message = person.[2]()
Drag options to blanks, or click blank then click option'
AString
Bgreet
CInt
DsayHello
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong return type or calling a method that does not exist.
5fill in blank
hard

Fill all three blanks to define a method with a parameter and call it with an argument.

Swift
struct Person {
    func greet([1] name: [2]) -> String {
        return "Hello, \(name)!"
    }
}

let person = Person()
let message = person.greet([3]: "Alice")
Drag options to blanks, or click blank then click option'
Afor
BString
C"Alice"
Dto
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names, types, or argument values.