Why structs are preferred in Swift
📖 Scenario: Imagine you are building a simple app to keep track of books in a library. You want to store information about each book, like its title and author. You need to decide whether to use a struct or a class in Swift to represent each book.
🎯 Goal: Learn why Swift prefers structs over classes for many tasks by creating a simple struct to represent books and seeing how it works.
📋 What You'll Learn
Create a struct called
Book with two properties: title and author, both of type String.Create a variable called
myBook and assign it a Book instance with title "Swift Basics" and author "Jane Doe".Create a copy of
myBook called copiedBook and change its title to "Advanced Swift".Print both
myBook and copiedBook titles to show they are different.💡 Why This Matters
🌍 Real World
Apps often store data like user profiles, settings, or items using structs for safety and speed.
💼 Career
Understanding when to use structs helps you write better Swift code, a key skill for iOS development jobs.
Progress0 / 4 steps