Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than 'greet'.
✗ Incorrect
The method name should be greet as required.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a method name that does not exist.
✗ Incorrect
To call the method defined earlier, use greet().
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect external parameter names like 'with' or 'to'.
✗ Incorrect
The correct external parameter name is for to read naturally as greet(for name: String).
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong return type or calling a method that does not exist.
✗ Incorrect
The method returns a String and is called greet().
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names, types, or argument values.
✗ Incorrect
The method uses external parameter name for, parameter type String, and is called with the string "Alice".