📖 Scenario: You are creating a simple app to manage information about books. Each book has a title and an author. Sometimes, you only know the title and want to create a book with a default author.
🎯 Goal: Build a Book class with a main initializer and a convenience initializer that sets a default author when only the title is known.
📋 What You'll Learn
Create a Book class with properties title and author
Write a main initializer that takes title and author as parameters
Add a convenience initializer that takes only title and sets author to "Unknown"
Create an instance using the convenience initializer and print its details
💡 Why This Matters
🌍 Real World
Convenience initializers help create objects easily when some information is missing or default values are common, like creating user profiles or product items.
💼 Career
Understanding initializers and convenience initializers is essential for Swift developers building apps with clean and reusable code.
Progress0 / 4 steps
1
Create the Book class with properties and main initializer
Create a class called Book with two String properties: title and author. Write an initializer init(title: String, author: String) that sets these properties.
Swift
Hint
Remember to use self to assign the parameters to the properties inside the initializer.
2
Add a convenience initializer with default author
Add a convenience init(title: String) initializer inside the Book class. It should call the main initializer with author set to "Unknown".
Swift
Hint
Use self.init inside the convenience initializer to call the main initializer.
3
Create a Book instance using the convenience initializer
Create a constant book1 using the convenience initializer with the title "Swift Programming".
Swift
Hint
Use let book1 = Book(title: "Swift Programming") to create the instance.
4
Print the book details
Print the title and author of book1 using print. Format the output as: "Title: Swift Programming, Author: Unknown".
Swift
Hint
Use string interpolation with \(book1.title) and \(book1.author) inside the print statement.
Practice
(1/5)
1. What is the main purpose of a convenience initializer in Swift?
easy
A. To provide an easier way to create an object by calling a designated initializer
B. To override a superclass initializer
C. To create a completely new object without calling any other initializer
D. To deallocate an object before it is destroyed
Solution
Step 1: Understand the role of convenience initializers
Convenience initializers are shortcuts to create objects more easily by calling a designated initializer.
Step 2: Compare options with this role
Only To provide an easier way to create an object by calling a designated initializer describes this purpose correctly; others describe unrelated concepts.
Final Answer:
To provide an easier way to create an object by calling a designated initializer -> Option A
2. Which of the following is the correct syntax for a convenience initializer in Swift?
easy
A. convenience func init(name: String) { self.init() }
B. init convenience(name: String) { self.init() }
C. func convenience init(name: String) { self.init() }
D. convenience init(name: String) { self.init() }
Solution
Step 1: Recall the correct syntax for convenience initializers
In Swift, the keyword convenience comes before init without any function keyword.
Step 2: Check each option
convenience init(name: String) { self.init() } matches the correct syntax. Options B, C, and D misuse keywords or order.
Final Answer:
convenience init(name: String) { self.init() } -> Option D
Quick Check:
Keyword order: convenience before init [OK]
Hint: Remember: 'convenience' keyword comes before 'init' [OK]
Common Mistakes:
Placing 'convenience' after 'init'
Using 'func' keyword with initializers
Incorrect keyword order causing syntax errors
3. Consider the following Swift code:
class Car {
var color: String
init(color: String) {
self.color = color
}
convenience init() {
self.init(color: "Red")
}
}
let myCar = Car()
What is the value of myCar.color?
medium
A. nil
B. "Red"
C. "" (empty string)
D. Compilation error
Solution
Step 1: Understand the initializers in the Car class
The designated initializer sets the color property. The convenience initializer calls the designated one with "Red".
Step 2: Analyze the object creation
Creating myCar with Car() calls the convenience initializer, which sets color to "Red".
Final Answer:
"Red" -> Option B
Quick Check:
Convenience init sets color to "Red" [OK]
Hint: Convenience init calls designated init with default values [OK]
Common Mistakes:
Assuming color is empty or nil without initialization
Thinking convenience init does not call designated init
Expecting compilation error due to missing default init
4. Identify the error in this Swift class using a convenience initializer:
class Person {
var name: String
convenience init() {
self.name = "Unknown"
}
init(name: String) {
self.name = name
}
}
medium
A. Convenience initializer must call a designated initializer, not assign properties directly
B. Designated initializer must be marked as convenience
C. Property 'name' must be optional
D. No error, code is correct
Solution
Step 1: Recall rules for convenience initializers
A convenience initializer must call another initializer in the same class, not assign properties directly.
Step 2: Check the convenience init implementation
Here, self.name = "Unknown" is assigned directly, which is not allowed.
Final Answer:
Convenience initializer must call a designated initializer, not assign properties directly -> Option A
Quick Check:
Convenience init must call designated init [OK]
Hint: Convenience init cannot set properties directly; must call designated init [OK]
Common Mistakes:
Assigning properties directly in convenience initializer
Marking designated initializer as convenience
Making properties optional unnecessarily
5. You want to create a Swift class Rectangle with a designated initializer that takes width and height, and a convenience initializer that creates a square by taking only one side length. Which of the following implementations correctly uses a convenience initializer?
hard
A. class Rectangle {
var width: Double
var height: Double
init(width: Double, height: Double) {
self.width = width
self.height = height
}
convenience init(side: Double) {
self.width = side
self.height = side
}
}
B. class Rectangle {
var width: Double
var height: Double
convenience init(width: Double, height: Double) {
self.width = width
self.height = height
}
init(side: Double) {
self.width = side
self.height = side
}
}
C. class Rectangle {
var width: Double
var height: Double
init(width: Double, height: Double) {
self.width = width
self.height = height
}
convenience init(side: Double) {
self.init(width: side, height: side)
}
}
D. class Rectangle {
var width: Double
var height: Double
init(width: Double, height: Double) {
self.width = width
self.height = height
}
convenience init(side: Double) {
self.height = side
}
}
Solution
Step 1: Identify designated and convenience initializers
The designated initializer takes width and height. The convenience initializer should call the designated one.
Step 2: Check each option for correct usage
class Rectangle {
var width: Double
var height: Double
init(width: Double, height: Double) {
self.width = width
self.height = height
}
convenience init(side: Double) {
self.init(width: side, height: side)
}
} correctly defines the designated init and the convenience init calls it with equal sides. Others either misuse convenience keyword or assign properties directly.
Final Answer:
class Rectangle {
var width: Double
var height: Double
init(width: Double, height: Double) {
self.width = width
self.height = height
}
convenience init(side: Double) {
self.init(width: side, height: side)
}
} -> Option C
Quick Check:
Convenience init calls designated init with side values [OK]
Hint: Convenience init must call designated init with all required parameters [OK]
Common Mistakes:
Marking designated initializer as convenience
Assigning properties directly in convenience initializer
Not calling designated initializer from convenience initializer
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.