Agent skill
swiftui-data
Implement data persistence with SwiftData, Core Data, CloudKit sync, and local storage. Use when storing app data, creating database models, syncing with iCloud, or managing user preferences with AppStorage.
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/data/swiftui-data
SKILL.md
SwiftUI Data Persistence
SwiftData (iOS 17+) - Recommended
import SwiftData
@Model
final class Task {
var title: String
var isCompleted: Bool
var createdAt: Date
var priority: Priority
@Relationship(deleteRule: .cascade)
var subtasks: [Subtask]?
init(title: String, priority: Priority = .medium) {
self.title = title
self.isCompleted = false
self.createdAt = .now
self.priority = priority
}
}
enum Priority: String, Codable, CaseIterable {
case low, medium, high
}
// App configuration
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(for: Task.self)
}
}
SwiftData Queries
struct TaskListView: View {
@Query(
filter: #Predicate<Task> { !$0.isCompleted },
sort: \Task.createdAt,
order: .reverse
)
private var tasks: [Task]
@Environment(\.modelContext) private var context
var body: some View {
List(tasks) { task in
TaskRow(task: task)
.swipeActions {
Button("Delete", role: .destructive) {
context.delete(task)
}
}
}
}
func addTask(_ title: String) {
let task = Task(title: title)
context.insert(task)
}
}
CloudKit Sync with SwiftData
// Enable in App configuration
@main
struct MyApp: App {
var container: ModelContainer
init() {
let schema = Schema([Task.self, Subtask.self])
let config = ModelConfiguration(
schema: schema,
cloudKitDatabase: .private("iCloud.com.myapp")
)
container = try! ModelContainer(
for: schema,
configurations: [config]
)
}
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(container)
}
}
AppStorage & SceneStorage
struct SettingsView: View {
// Persists to UserDefaults
@AppStorage("username") private var username = ""
@AppStorage("isDarkMode") private var isDarkMode = false
@AppStorage("fontSize") private var fontSize = 14.0
// Persists per-scene (window state)
@SceneStorage("selectedTab") private var selectedTab = 0
var body: some View {
Form {
TextField("Username", text: $username)
Toggle("Dark Mode", isOn: $isDarkMode)
Slider(value: $fontSize, in: 10...24)
}
}
}
When to Use What
| Solution | Use Case |
|---|---|
@AppStorage |
Simple preferences, settings |
@SceneStorage |
Window/scene-specific UI state |
| SwiftData | Complex models, relationships, CloudKit |
| Core Data | iOS < 17, advanced migrations |
| Keychain | Sensitive data (tokens, passwords) |
| FileManager | Documents, large files, exports |
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?