热门面试题与答案和在线测试
面向面试准备、在线测试、教程与实战练习的学习平台

通过聚焦学习路径、模拟测试和面试实战内容持续提升技能。

WithoutBook 将分主题面试题、在线练习测试、教程和对比指南整合到一个响应式学习空间中。

Chapter 8

Persistence with UserDefaults, Files, Codable, Core Data, SwiftData, and Keychain

Store app data correctly by learning which storage option fits which problem, from tiny settings to offline domain models and secure credentials.

Inside this chapter

  1. Choosing the Right Persistence Tool
  2. UserDefaults for Simple Preferences
  3. Files and Codable
  4. Core Data and SwiftData
  5. Keychain for Sensitive Information
  6. Offline-First Thinking

Series navigation

Study the chapters in order for the clearest path from setup and Swift basics to architecture, release management, and advanced iOS engineering. Use the navigation at the bottom to move smoothly across the full tutorial series.

Tutorial Home

Chapter 8

Choosing the Right Persistence Tool

Not all data should be stored in the same way. Small preferences, large documents, cached API responses, user profiles, secure tokens, and relational business data each have different storage needs. Good persistence design begins with the question: what kind of data is this?

Chapter 8

UserDefaults for Simple Preferences

UserDefaults.standard.set(true, forKey: "hasSeenOnboarding")
let seen = UserDefaults.standard.bool(forKey: "hasSeenOnboarding")

UserDefaults is appropriate for tiny configuration flags and preferences. It is not a general-purpose database and should not hold large structured domain models.

Chapter 8

Files and Codable

struct DraftNote: Codable {
    let title: String
    let body: String
}

let encoder = JSONEncoder()
let data = try encoder.encode(DraftNote(title: "Trip", body: "Book hotel"))

File-based persistence is useful for exported reports, cached payloads, drafts, offline reference content, and media-related workflows.

Chapter 8

Core Data and SwiftData

Core Data is a mature object graph and persistence framework used heavily in iOS applications. SwiftData is a modern layer that simplifies parts of model persistence for newer SwiftUI-oriented codebases. Students should understand concepts such as entities, relationships, fetches, context changes, migrations, and performance implications.

Real examples include offline shopping carts, note-taking apps, CRM field records, health logs, and saved article libraries.

Chapter 8

Keychain for Sensitive Information

Authentication tokens, refresh secrets, and highly sensitive credentials should go in Keychain, not in plain files or UserDefaults. Security architecture matters because mobile devices can be lost, shared, or inspected.

Chapter 8

Offline-First Thinking

A strong iOS app should keep the user productive even during poor connectivity. Persistence is not just about saving data. It is also about modeling sync boundaries, conflict resolution, stale cache detection, and user trust.

版权所有 © 2026,WithoutBook。