Swift UI数据存储

一. @StateObject 数据存储机制

@StateObject 保存的数据存储在设备内存(RAM)中,是临时存储

swift 复制代码
import SwiftUI
internal import Combine
class BloodGlucoseStore: ObservableObject{
    @Published var count = 0 // 存储在内存中
    
}

struct JLHomeView: View {
    @StateObject private var store = BloodGlucoseStore()// 对象存在于内存中
    var body: some View {
        Text("记录数量:\(store.count)")
        Button("点击") {
            store.count += 1
        }
        
    }
}

数据生命周期

  • 创建时机:视图第一次被创建时
  • 保持时机:视图重新渲染时数据保持不变
  • 销毁时机:视图被销毁时数据丢失
swift 复制代码
struct ContentView: View {
    @State private var showHomeView = false
    
    var body: some View {
        VStack {
            Button("显示/隐藏 HomeView") {
                showHomeView.toggle()
            }
            
            if showHomeView {
                JLHomeView()  // 创建时:数据在内存中创建
            }              // 销毁时:数据从内存中清除
        }
    }
}

二. UserDefaults 存储机制

存储位置

  • 📁 应用沙盒中的 .plist 文件
  • 路径:/Library/Preferences/[Bundle-ID].plist

UserDefaults 数据安全性

✅ 不会丢失的情况
  • 应用更新:数据保持不变
  • 应用重启:数据依然存在
  • 设备重启:数据保持不变
  • iOS 系统更新:数据通常保持
❌ 会丢失的情况
  • 卸载应用:整个应用沙盒被删除
  • 恢复设备但不恢复备份:数据丢失
  • 手动清除应用数据:通过系统设置清除
Swift 复制代码
class SettingStore: ObservableObject{
    
    @Published var isDarmMode: Bool{
        didSet{
            /// 保存数据
            UserDefaults.standard.set(isDarmMode, forKey: "isDarmMode")
            UserDefaults.standard.synchronize()
        }
    }
    
    init(){
        /// 读数数据
        isDarmMode = UserDefaults.standard.bool(forKey: "isDarmMode")
    }
    deinit{
        /// 删除数据
        UserDefaults.standard.removeObject(forKey: "isDarmMode")
        UserDefaults.standard.synchronize()
    }
}

三. @Published 属性包装器

核心作用

@Published 的主要作用是自动触发 UI 更新

csharp 复制代码
class CounterStore: ObservableObject {
    var count = 0  // 普通属性
    
    func increment() {
        count += 1  // UI 不会更新!
    }
}

实际应用示例

less 复制代码
class BloodGlucoseStore: ObservableObject {
    @Published var records: [BloodGlucoseRecord] = []
    @Published var isLoading = false
    @Published var errorMessage: String?
    @Published var selectedDate = Date()
    @Published var filterType: FilterType = .all
    
    // 计算属性也会响应 @Published 属性的变化
    var filteredRecords: [BloodGlucoseRecord] {
        switch filterType {
        case .all:
            return records
        case .today:
            return records.filter { Calendar.current.isDateInToday($0.date) }
        case .thisWeek:
            return records.filter { $0.date.isInCurrentWeek }
        }
    }
    
    func addRecord(_ record: BloodGlucoseRecord) {
        records.append(record)  // 触发 UI 更新
    }
    
    func setFilter(_ filter: FilterType) {
        filterType = filter  // 触发筛选更新
    }
}

高级用法

自定义 setter
swift 复制代码
class UserStore: ObservableObject {
    @Published var username: String = "" {
        didSet {
            validateUsername()
            saveToUserDefaults()
        }
    }
    
    @Published var isUsernameValid = false
    
    private func validateUsername() {
        isUsernameValid = username.count >= 3
    }
}
级联更新
scss 复制代码
class ShoppingCartStore: ObservableObject {
    @Published var items: [CartItem] = [] {
        didSet {
            updateTotalPrice()  // items 变化时自动更新总价
        }
    }
    
    @Published var totalPrice: Double = 0
    @Published var discountCode: String = "" {
        didSet {
            updateTotalPrice()  // 折扣码变化时也更新总价
        }
    }
    
    private func updateTotalPrice() {
        let subtotal = items.reduce(0) { $0 + $1.price * Double($1.quantity) }
        let discount = calculateDiscount(for: discountCode)
        totalPrice = subtotal - discount
    }
}
相关推荐
Digitally2 小时前
无需 iTunes 将文件从 PC 传输到 iPhone
ios·iphone
1024小神4 小时前
xcode 配置了AppIcon 但是不显示icon图标
ios·swiftui·swift
2501_915918416 小时前
iOS 项目中证书管理常见的协作问题
android·ios·小程序·https·uni-app·iphone·webview
ITKEY_6 小时前
iOS网页应用无地址栏无工具栏
ios
2501_915918416 小时前
提升 iOS 应用安全审核通过率的一种思路,把容易被拒的点先处理
android·安全·ios·小程序·uni-app·iphone·webview
RollingPin7 小时前
iOS探究使用Block方式实现一对多回调能力
ios·block·runtime·数据分发·解耦·动态绑定·一对多回调
TheNextByte17 小时前
iPhone短信备份与恢复:3种最佳方法及短信备份与恢复应用
ios·iphone
2501_916008897 小时前
iOS 应用发布流程中常被忽视的关键环节
android·ios·小程序·https·uni-app·iphone·webview
ForteScarlet7 小时前
Kotlin 2.3.0 现已发布!又有什么好东西?
android·开发语言·后端·ios·kotlin
往来凡尘21 小时前
Flutter运行iOS26真机的两个问题
flutter·ios