An initializer is a special method that prepares an instance of a class, struct, or enum for use by setting initial values for its properties.
Click to reveal answer
intermediate
What is a designated initializer?
A designated initializer is the primary initializer for a class that fully initializes all properties introduced by that class and calls an appropriate superclass initializer.
Click to reveal answer
intermediate
How does a designated initializer differ from a convenience initializer?
A designated initializer initializes all properties and calls a superclass initializer, while a convenience initializer provides shortcuts and must call another initializer from the same class.
Click to reveal answer
intermediate
Why must a designated initializer call a superclass initializer?
Because the superclass may have properties that need initialization, calling its initializer ensures the entire inheritance chain is properly set up.
Click to reveal answer
beginner
What happens if you don’t provide any initializer in a Swift class?
Swift provides a default initializer if all properties have default values and no custom initializers are defined.
Click to reveal answer
Which initializer must fully initialize all properties introduced by its class?
ADesignated initializer
BConvenience initializer
CDefault initializer
DFailable initializer
✗ Incorrect
A designated initializer fully initializes all properties introduced by its class.
In Swift, a convenience initializer must call:
AA designated initializer from the same class
BA superclass initializer
CItself recursively
DNo other initializer
✗ Incorrect
Convenience initializers must call another initializer from the same class, usually a designated initializer.
What is the main role of a designated initializer in a class hierarchy?
ATo deinitialize instances
BTo provide shortcuts for initialization
CTo initialize all properties and call the superclass initializer
DTo override superclass methods
✗ Incorrect
Designated initializers initialize all properties and call the superclass initializer to ensure proper setup.
If a Swift class has all properties with default values and no custom initializers, what initializer is available?
ADesignated initializer
BDefault initializer
CConvenience initializer
DNo initializer
✗ Incorrect
Swift automatically provides a default initializer in this case.
Which of the following is true about designated initializers?
AThey can only be used in structs
BThey are optional
CThey cannot call other initializers
DThey must call a superclass designated initializer
✗ Incorrect
Designated initializers must call a superclass designated initializer to ensure proper initialization.
Explain the difference between a designated initializer and a convenience initializer in Swift.
Think about who does the full setup and who provides easier ways to create instances.
You got /4 concepts.
Describe why calling a superclass initializer is important in a designated initializer.
Consider what happens if the superclass is not properly set up.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a designated initializer in Swift?
easy
A. To fully initialize all properties of a class before use
B. To provide default values for optional properties only
C. To create a copy of an existing object
D. To deallocate memory when an object is no longer needed
Solution
Step 1: Understand what initializers do
Initializers set starting values for new objects to ensure they are ready to use.
Step 2: Identify the role of designated initializers
Designated initializers fully initialize all properties of a class, making the object safe and complete.
Final Answer:
To fully initialize all properties of a class before use -> Option A
Quick Check:
Designated initializer = full property initialization [OK]
Hint: Designated init always sets all properties fully [OK]
Common Mistakes:
Confusing designated init with convenience init
Thinking initializers copy objects
Mixing initialization with deallocation
2. Which of the following is the correct syntax for a designated initializer in a Swift class?
easy
A. func init() { }
B. init() { }
C. constructor() { }
D. initialize() { }
Solution
Step 1: Recall Swift initializer syntax
In Swift, initializers use the keyword init without a return type or func keyword.
Step 2: Identify the correct syntax
The correct syntax is init() { }. Other options use incorrect keywords or syntax.
Final Answer:
init() { } -> Option B
Quick Check:
Swift designated init syntax = init() { } [OK]
Hint: Swift init uses just 'init', no func or constructor [OK]
Common Mistakes:
Using 'func' keyword with init
Using 'constructor' or 'initialize' instead of 'init'
Adding return types to init
3. Consider this Swift class:
class Person {
var name: String
var age: Int
init(name: String) {
self.name = name
self.age = 0
}
}
What will be the value of age after creating let p = Person(name: "Anna")?
medium
A. Name of the person
B. nil
C. Undefined
D. 0
Solution
Step 1: Analyze the initializer code
The initializer sets self.name = name and explicitly sets self.age = 0.
Step 2: Determine the value of age after initialization
Since age is set to 0 inside the initializer, the new object will have age = 0.
Final Answer:
0 -> Option D
Quick Check:
Explicit age assignment = 0 [OK]
Hint: Check initializer assignments for property values [OK]
Common Mistakes:
Assuming age is nil because not passed as parameter
Thinking age is uninitialized or undefined
Confusing name with age value
4. Identify the error in this Swift class initializer:
class Car {
var model: String
var year: Int
init(model: String) {
model = model
year = 2020
}
}
medium
A. Initializer missing return type
B. Class must inherit from NSObject
C. Properties are not initialized using self keyword
D. year property cannot be assigned a value
Solution
Step 1: Check property assignments in initializer
The code assigns model = model and year = 2020 without self., so it assigns parameters to themselves, not properties.
Properties must be assigned with self.model = model and self.year = 2020 to set the instance variables.
Final Answer:
Properties are not initialized using self keyword -> Option C
Quick Check:
Use self.property = value to initialize [OK]
Hint: Use self. to assign properties inside init [OK]
Common Mistakes:
Forgetting self. causes no property assignment
Adding return type to init
Thinking inheritance affects property assignment
5. You want to create a Swift class Book with properties title (String) and pages (Int). The designated initializer should allow setting both properties, but if pages is not provided, it defaults to 100. Which is the best way to write this initializer?
hard
A. init(title: String, pages: Int = 100) { self.title = title; self.pages = pages }
Step 1: Understand default parameter values in Swift
Swift allows default values in parameters, so pages: Int = 100 means pages defaults to 100 if not passed.
Step 2: Check initializer completeness and safety
The initializer init(title: String, pages: Int = 100) { self.title = title; self.pages = pages } sets both properties properly and safely with a default value. The version with only a title parameter cannot set pages explicitly. The version taking both parameters but only assigning title misses the pages assignment. The version using an optional Int? for pages with force unwrap is unsafe.
Final Answer:
init(title: String, pages: Int = 100) { self.title = title; self.pages = pages } -> Option A
Quick Check:
Default parameter = safe default value [OK]
Hint: Use default parameter values for optional init args [OK]
Common Mistakes:
Forgetting to assign all properties
Using force unwrap (!) unsafely
Not providing default value in parameter
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.