Complete the code to declare a struct named Person with a name property.
struct [1] {
var name: String
}The keyword Person is the name of the struct. We use struct to declare it.
Complete the code to create a new instance of the struct Person.
let person = [1](name: "Alice")
To create an instance, use the struct name Person followed by parentheses with parameters.
Fix the error in the code to correctly copy a struct instance.
var person1 = Person(name: "Bob") var person2 = person1 person2.name = [1]
Assigning a new string value like "Alice" to person2.name changes only person2 because structs are copied.
Fill both blanks to create a struct with a method that returns a greeting.
struct Greeter {
var name: String
func [1]() -> String {
return "Hello, \([2])!"
}
}The method is named greet and it returns a greeting using the name property.
Fill all three blanks to create a struct with a computed property that returns the full name.
struct User {
var firstName: String
var lastName: String
var fullName: String {
return [1] + " " + [2]
}
func printName() {
print([3])
}
}The computed property fullName combines firstName and lastName. The printName method prints fullName.