@Model 타입 정의하기
클래스를 영속 모델로 지정합니다.
@Model 타입 정의하기은(는) CoddyKit의 무료 SwiftUI Academy 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 SwiftUI Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. SwiftUI Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Meet SwiftData
SwiftData is Apple's modern way to save app data right on the device. You describe your data once, and it handles storage for you. 💾
The @Model Macro
You turn a plain class into a saved type by adding the @Model macro above it. That one line makes every instance persistable.
@Model
class Task {
}Stored Properties
Every stored property on a @Model class is saved automatically. No extra annotations are needed for basic values like text or numbers.
@Model
class Task {
var title: String
var isDone: Bool
}Models Are Classes
SwiftData models must be classes, not structs. SwiftData tracks them by reference so it can watch changes and save them.
Adding an Initializer
Give your model an init so you can create instances with starting values. This keeps your stored properties set when a task is made.
init(title: String, isDone: Bool = false) {
self.title = title
self.isDone = isDone
}Supported Property Types
SwiftData saves common types out of the box: String, Int, Double, Bool, Date, and Data all just work without setup.
var due: Date
var priority: IntOptional Properties
Mark a value optional when it may be missing. SwiftData stores nil happily, which is great for fields the user has not filled in yet.
var note: String?Unique Constraints
Add @Attribute(.unique) to stop duplicate values for a property. SwiftData will keep that field one-of-a-kind across your store.
@Attribute(.unique) var id: UUIDRelationships Between Models
Models can point to each other with a @Relationship. This links one record to another, like a project owning many tasks.
@Relationship var tasks: [Task]Cascade Deletes
A .cascade delete rule removes related records together. Delete a project and its tasks vanish too, keeping your data tidy.
@Relationship(deleteRule: .cascade)
var tasks: [Task]Ignoring a Property
Use @Transient for values you do not want saved, like a temporary cache. SwiftData skips these when writing to disk.
@Transient var isEditing = falseQuick Check
Quick check on defining models.
Recap: Defining Models
You learned that @Model turns a class into saved data, with normal properties, optionals, unique attributes, and relationships. Your data shape is ready. 🎉
자주 묻는 질문
“@Model 타입 정의하기” 강의는 무료인가요?
네 — “@Model 타입 정의하기” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 SwiftUI Academy 강의 전체를 잠금 해제할 수 있습니다. SwiftUI Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
“@Model 타입 정의하기”에서 뭘 배우나요?
클래스를 영속 모델로 지정합니다. 브라우저에서 직접 실행하는 실습 코드로 SwiftUI Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
SwiftUI Academy을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 SwiftUI Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.
“@Model 타입 정의하기” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 SwiftUI Academy 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 SwiftUI Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- @Model 타입 정의하기
- 모델 컨테이너와 컨텍스트
- @Query로 조회하기
- 삽입, 업데이트 및 삭제