Using Convenience Initializers in Swift
📖 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 authorWrite a main initializer that takes
title and author as parametersAdd 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