Complete the code to define a struct named Person with a name property.
struct Person {
var [1]: String
}The property to store the person's name should be called name.
Complete the code to create an instance of the Person struct named person1.
let person1 = [1](name: "Alice")
To create an instance of a struct, use its name Person followed by parentheses.
Fix the error in the code by choosing the correct keyword to define a class named Animal.
[1] Animal {
var species: String
}Classes are defined using the keyword class in Swift.
Fill both blanks to complete the code that shows how classes support inheritance but structs do not.
class Dog: [1] { var breed: String init(breed: String) { self.breed = breed } } struct [2] { var color: String }
The class Dog inherits from the class Animal. Structs like Car cannot inherit.
Fill all three blanks to complete the code that demonstrates value type behavior of structs and reference type behavior of classes.
struct Point {
var x: Int
var y: Int
}
var p1 = Point(x: 0, y: 0)
var p2 = p1
p2.x = [1]
class Location {
var name: String
init(name: String) {
self.name = name
}
}
var loc1 = Location(name: "Home")
var loc2 = loc1
loc2.name = [2]
print(p1.x) // [3]Changing p2.x does not affect p1.x because structs are value types. So p1.x remains 0. Changing loc2.name changes loc1.name because classes are reference types.