Initializers and Designated Init in Swift
📖 Scenario: You are creating a simple app to manage information about books in a library. Each book has a title and a number of pages.
🎯 Goal: Build a Swift struct called Book with a designated initializer that sets the title and pages. Then create an instance and print its details.
📋 What You'll Learn
Create a struct called
Book with two properties: title (String) and pages (Int).Add a designated initializer
init(title: String, pages: Int) to set these properties.Create a variable
myBook using the initializer with title "Swift Programming" and pages 350.Print the book's title and pages in the format:
"Title: Swift Programming, Pages: 350".💡 Why This Matters
🌍 Real World
Initializers help set up objects with the right starting values, just like filling out a form before using a new tool.
💼 Career
Understanding initializers is essential for building apps that manage data cleanly and safely, a key skill for Swift developers.
Progress0 / 4 steps