📖 Scenario: You are creating a simple app to keep track of books in a library. Each book has a title, author, and number of pages.
🎯 Goal: Build a Swift struct called Book and use the memberwise initializer to create book instances easily.
📋 What You'll Learn
Create a struct named Book with properties title, author, and pages
Create a variable firstBook using the memberwise initializer with exact values
Create a variable secondBook using the memberwise initializer with exact values
Print the details of both books using print
💡 Why This Matters
🌍 Real World
Structs with memberwise initializers are used to model simple data objects like books, users, or products in apps.
💼 Career
Understanding memberwise initializers helps you quickly create and manage data models in Swift, a key skill for iOS development.
Progress0 / 4 steps
1
Create the Book struct
Create a struct called Book with three properties: title of type String, author of type String, and pages of type Int.
Swift
Hint
Use struct Book { } and inside it declare three var properties with the exact names and types.
2
Create the first book instance
Create a variable called firstBook and initialize it using the memberwise initializer of Book with these exact values: title = "The Swift Guide", author = "John Appleseed", pages = 250.
Swift
Hint
Use the syntax Book(title: "The Swift Guide", author: "John Appleseed", pages: 250) to create firstBook.
3
Create the second book instance
Create a variable called secondBook and initialize it using the memberwise initializer of Book with these exact values: title = "Learning SwiftUI", author = "Jane Developer", pages = 300.
Swift
Hint
Use the memberwise initializer with the exact values to create secondBook.
4
Print the book details
Print the details of firstBook and secondBook using two separate print statements. Use string interpolation to show the title, author, and pages in this format: "Title: [title], Author: [author], Pages: [pages]".
Swift
Hint
Use print("Title: \(firstBook.title), Author: \(firstBook.author), Pages: \(firstBook.pages)") and similarly for secondBook.
Practice
(1/5)
1. What does a memberwise initializer in Swift do for a struct?
easy
A. It creates a function to print all properties.
B. It deletes all properties from the struct.
C. It automatically creates an initializer that sets all properties at once.
D. It converts the struct into a class.
Solution
Step 1: Understand memberwise initializer purpose
A memberwise initializer lets you create an instance by setting all properties in one call.
Step 2: Recall Swift default behavior for structs
Swift automatically provides this initializer for structs without custom initializers.
Final Answer:
It automatically creates an initializer that sets all properties at once. -> Option C
Quick Check:
Memberwise initializer = sets all properties at once [OK]
Hint: Memberwise initializer sets all struct properties at once [OK]
Common Mistakes:
Thinking it prints properties
Confusing with class initializers
Assuming it deletes properties
2. Which of the following is the correct way to use a memberwise initializer for this struct?
struct Book {
var title: String
var pages: Int
}
easy
A. let myBook = Book(title -> "Swift Guide", pages -> 300)
B. let myBook = Book("Swift Guide", 300)
C. let myBook = Book(title = "Swift Guide", pages = 300)
D. let myBook = Book(title: "Swift Guide", pages: 300)
Solution
Step 1: Recall memberwise initializer syntax
It uses parameter labels matching property names: title: and pages:.
Step 2: Check each option
let myBook = Book(title: "Swift Guide", pages: 300) uses correct syntax with labels and values. Others use wrong syntax or missing labels.
Final Answer:
let myBook = Book(title: "Swift Guide", pages: 300) -> Option D
Quick Check:
Memberwise initializer syntax = labels and values [OK]
Hint: Use property names as labels in initializer call [OK]
Common Mistakes:
Omitting labels in initializer
Using = instead of : in parameters
Using arrows -> instead of colons
3. What will be the output of this Swift code?
struct Point {
var x: Int
var y: Int
}
let p = Point(x: 5, y: 10)
print(p.x + p.y)
medium
A. 510
B. 15
C. x: 5, y: 10
D. Error: No memberwise initializer
Solution
Step 1: Understand memberwise initializer usage
The struct Point has two Int properties and uses the memberwise initializer to set x=5 and y=10.
Step 2: Calculate the print output
p.x + p.y = 5 + 10 = 15, so print outputs 15.
Final Answer:
15 -> Option B
Quick Check:
5 + 10 = 15 [OK]
Hint: Add property values after initializing with memberwise initializer [OK]
Common Mistakes:
Concatenating numbers as strings
Expecting a print of property names
Assuming no initializer exists
4. Identify the error in this code using a memberwise initializer:
struct Car {
var brand: String
var year: Int
}
let myCar = Car(brand: "Toyota")
medium
A. Missing value for 'year' property in initializer
B. Incorrect property name 'brand'
C. Cannot create instance of struct
D. Syntax error in struct declaration
Solution
Step 1: Check memberwise initializer requirements
All properties must be initialized. 'year' is missing in the initializer call.
Step 2: Identify error cause
Since 'year' is not given a value, the code will cause a compile error.
Final Answer:
Missing value for 'year' property in initializer -> Option A
Quick Check:
All properties must be set in memberwise initializer [OK]
Hint: Provide all properties when using memberwise initializer [OK]
Common Mistakes:
Forgetting to initialize all properties
Assuming default values without setting
Misnaming properties in initializer
5. Given this struct with a default property value:
struct User {
var name: String
var age: Int = 18
}
Which initializer call is valid and why?
hard
A. let u = User(name: "Anna") // age uses default value
B. let u = User(name: "Anna", age: 20) // must always provide age
C. let u = User() // no parameters allowed
D. let u = User(age: 18) // name can be omitted
Solution
Step 1: Understand default property values in structs
Properties with default values can be omitted in memberwise initializer calls.
Step 2: Analyze each initializer call
let u = User(name: "Anna") // age uses default value provides 'name' only; 'age' uses default 18, which is valid. Others omit required 'name' or incorrectly require all parameters.
Final Answer:
let u = User(name: "Anna") // age uses default value -> Option A