Introduction
We use @Model to create simple data containers that hold information in our app. It helps keep data organized and easy to use.
We use @Model to create simple data containers that hold information in our app. It helps keep data organized and easy to use.
@Model
struct ModelName {
var property1: Type
var property2: Type
}The @Model attribute tells Swift that this struct is a data model.
Properties inside the model hold the data you want to store.
@Model
struct User {
var name: String
var age: Int
}@Model
struct Task {
var title: String
var isDone: Bool
}This app shows a simple user model with name and age displayed on screen.
import SwiftUI @Model struct User { var name: String var age: Int } struct ContentView: View { @Model private var user = User(name: "Alice", age: 30) var body: some View { VStack(spacing: 20) { Text("Name: \(user.name)") Text("Age: \(user.age)") } .padding() } }
Use @Model to make your data easy to manage and update.
Models help keep your app organized by separating data from views.
@Model marks a struct as a data container.
Models hold properties that store your app's data.
Using models makes your app easier to build and maintain.
@Model in SwiftUI?@Model class Task { var title: String; var done: Bool }let task = Task(title: "Buy milk", done: false) print(task.done)
@Model class Product {
var name: String
var price: Double
func discount() -> Double {
return price * 0.9
}
}Pass the test and this pattern moves unknown