Complete the code to create a struct with a memberwise initializer.
struct Person {
var name: String
var age: Int
}
let person = Person([1]: "Alice", age: 30)The memberwise initializer uses the property names as parameter labels. Here, name is the correct label for the first argument.
Complete the code to create an instance of the struct using the memberwise initializer.
struct Car {
var make: String
var year: Int
}
let car = Car(make: "Toyota", [1]: 2020)The memberwise initializer requires the property names as labels. Here, year is the correct label for the second argument.
Fix the error in the code by completing the memberwise initializer call.
struct Book {
var title: String
var pages: Int
}
let book = Book(title: "Swift Guide", [1]: 250)The memberwise initializer uses the property names as labels. The property is called pages, so that label must be used.
Fill both blanks to create a struct instance using the memberwise initializer.
struct Rectangle {
var width: Double
var height: Double
}
let rect = Rectangle([1]: 10.5, [2]: 20.0)The memberwise initializer uses the property names as labels. The properties are width and height.
Fill all three blanks to create a struct instance with the memberwise initializer.
struct Student {
var name: String
var grade: Int
var passed: Bool
}
let student = Student([1]: "John", [2]: 9, [3]: true)The memberwise initializer uses the property names as labels. The properties are name, grade, and passed.