在 UserDefaults、Keychain、文件系统、内存缓存 中存储持久数据或文件。
支持所有
Codable
类型的持久化。
这些类型包括标准库类型如 String、Int;以及 Foundation 类型如 Date、Data、URL 等。
值可以以 .html、.json、.txt、.jpg、.png、.mov 和 .mp4 类型存储在文件系统中。
- iOS 11.0+
- Swift 5.0+
CocoaPods
将以下行添加到您的 Podfile
pod 'OYStore'
Swift 包管理器
将 OYStore 添加为 Package.swift
的依赖项,并将 OYStore 指定为目标依赖项
import PackageDescription
let package = Package(
name: "YOUR_PROJECT_NAME",
targets: [],
dependencies: [
.package(url: "https://github.com/osmanyildirim/OYStore", .upToNextMinor(from: "1.0")),
],
targets: [
.target(
name: "YOUR_PROJECT_NAME",
dependencies: ["OYStore"])
]
)
- 保存值
try OYStore.save(to: .userDefaults(key: "ud_string_value_key"), value: "ud_string_value")
try OYStore.save(to: .userDefaults(key: "ud_codable_value_key"), value: User())
try OYStore.save(to: .userDefaults(key: "ud_uiimage_value_key"), value: UIImage(named: "Sample")?.pngData())
- 获取值
let value: User = try OYStore.value(of: .userDefaults(key: "ud_codable_value_key"))
- 带有默认值的值获取
OYStore.value(of: .userDefaults(key: "ud_string_value_key"), default: "ud_default_value")
- 删除值
try OYStore.remove(of: .userDefaults(key: "ud_uiimage_value_key"))
- 删除所有值
try OYStore.removeAll(of: .userDefaults)
- 保存值
try OYStore.save(to: .keychain(key: "kc_string_value_key"), value: "kc_string_value")
try OYStore.save(to: .keychain(key: "kc_codable_value_key"), value: User())
try OYStore.save(to: .keychain(key: "kc_uiimage_value_key"), value: UIImage(named: "Sample")?.pngData())
- 获取值
let value: User = try OYStore.value(of: .keychain(key: "kc_codable_value_key"))
- 带有默认值的值获取
OYStore.value(of: .keychain(key: "kc_string_value_key"), default: "kc_default_value")
- 删除值
try OYStore.remove(of: .keychain(key: "kc_uiimage_value_key"))
- 删除所有值
try OYStore.removeAll(of: .keychain)
由 NSCache 支持,用于在内存中存储值
- 保存值
try OYStore.save(to: .memoryCache(key: "mc_string_value_key"), value: "mc_string_value")
try OYStore.save(to: .memoryCache(key: "mc_codable_value_key"), value: User())
try OYStore.save(to: .memoryCache(key: "mc_uiimage_value_key"), value: UIImage(named: "Sample")?.pngData())
- 获取值
let value: User = try OYStore.value(of: .memoryCache(key: "mc_codable_value_key"))
- 带有默认值的值获取
OYStore.value(of: .memoryCache(key: "mc_string_value_key"), default: "mc_default_value")
- 删除值
try OYStore.remove(of: .memoryCache(key: "mc_uiimage_value_key"))
- 删除所有值
try OYStore.removeAll(of: .memoryCache)
URLRequest
缓存
- 缓存响应
let session = URLSession.shared
let request = URLRequest(url: URL(string: "API_URL")!)
session.dataTask(with: request) { data, response, error in
try? OYStore.save(to: .urlCache(urlRequest: request, data: data, urlSession: session, urlResponse: response))
}.resume()
- 获取缓存的响应
let request = URLRequest(url: URL(string: "API_URL")!)
let value: User = try OYStore.value(of: .urlCache(urlRequest: request))
- 删除缓存的响应
let request = URLRequest(url: URL(string: "API_URL")!)
OYStore.remove(of: .urlCache(urlRequest: request))
- 删除所有缓存的响应
try? OYStore.removeAll(of: .urlCache)
Library/Caches
目录
请注意,系统可能会删除
Caches
目录以释放磁盘空间,因此您的应用程序必须能够根据需要重新创建或下载这些文件。
- 保存值
try OYStore.save(to: .diskCache(file: "dc_text_file", type: .txt), value: "dc_text_value")
try OYStore.save(to: .diskCache(file: "dc_html_file", type: .html), value: "dc_html_value")
try OYStore.save(to: .diskCache(file: "dc_image", type: .png), value: UIImage(named: "Sample")?.pngData())
... 以文件夹方式保存值...
try OYStore.save(to: .diskCache(file: "dc_parent_folder/dc_image", type: .png), value: UIImage(named: "Sample")?.pngData())
- 获取值
let value: String = try OYStore.value(of: .diskCache(file: "dc_text_file", type: .txt))
- 带有默认值的值获取
OYStore.value(of: .diskCache(file: "dc_text_file", type: .txt), default: "dc_default_value")
- 删除值
try OYStore.remove(of: .diskCache(key: "dc_uiimage_value_key"))
- 删除所有值
try OYStore.removeAll(of: .diskCache)
Library/Application Support
目录
将需要但永不应对用户可见的文件(如应用程序的数据库文件)存储在此处。您可以在顶层存储文件或创建子目录。目录的内容会被持久化并包含在 iCloud 和 iTunes 备份中。
- 保存值
try OYStore.save(to: .applicationSupport(file: "as_text_file", type: .txt), value: "as_text_value")
try OYStore.save(to: .applicationSupport(file: "as_html_file", type: .html), value: "as_html_value")
try OYStore.save(to: .applicationSupport(file: "as_image", type: .png), value: UIImage(named: "Sample")?.pngData())
... 以文件夹方式保存值...
try OYStore.save(to: .applicationSupport(file: "as_parent_folder/as_image", type: .png), value: UIImage(named: "Sample")?.pngData())
- 获取值
let value: String = try OYStore.value(of: .applicationSupport(file: "as_text_file", type: .txt))
- 带有默认值的值获取
OYStore.value(of: .applicationSupport(file: "as_text_file", type: .txt), default: "as_default_value")
- 删除值
try OYStore.remove(of: .applicationSupport(key: "as_uiimage_value_key"))
- 删除所有值
try OYStore.removeAll(of: .applicationSupport)
Documents
目录
如果启用了 iCloud 备份设置,iOS 设备会自动备份存储在
Documents
目录中的用户生成文档和其他数据。当用户设置新设备或重置现有设备时,可以恢复数据。
- 保存值
try OYStore.save(to: .documents(file: "d_text_file", type: .txt), value: "d_text_value")
try OYStore.save(to: .documents(file: "d_html_file", type: .html), value: "d_html_value")
try OYStore.save(to: .documents(file: "d_image", type: .png), value: UIImage(named: "Sample")?.pngData())
... 以文件夹方式保存值...
try OYStore.save(to: .documents(file: "d_parent_folder/d_image", type: .png), value: UIImage(named: "Sample")?.pngData())
- 获取值
let value: String = try OYStore.value(of: .documents(file: "d_text_file", type: .txt))
- 带有默认值的值获取
OYStore.value(of: .documents(file: "d_text_file", type: .txt), default: "d_default_value")
- 删除值
try OYStore.remove(of: .documents(key: "d_uiimage_value_key"))
- 删除所有值
try OYStore.removeAll(of: .documents)
tmp
目录
仅临时使用的数据应存储在
tmp
目录中。虽然这些文件不会被备份到 iCloud,但请记住在完成使用后删除这些文件,以防它们继续占用用户设备的空间。
- 保存值
try OYStore.save(to: .temporary(file: "tmp_text_file", type: .txt), value: "tmp_text_value")
try OYStore.save(to: .temporary(file: "tmp_html_file", type: .html), value: "tmp_html_value")
try OYStore.save(to: .temporary(file: "tmp_image", type: .png), value: UIImage(named: "Sample")?.pngData())
... 以文件夹方式保存值...
try OYStore.save(to: .temporary(file: "d_parent_folder/tmp_image", type: .png), value: UIImage(named: "Sample")?.pngData())
- 获取值
let value: String = try OYStore.value(of: .temporary(file: "tmp_text_file", type: .txt))
- 带有默认值的值获取
OYStore.value(of: .temporary(file: "tmp_text_file", type: .txt), default: "tmp_default_value")
- 删除值
try OYStore.remove(of: .temporary(key: "tmp_uiimage_value_key"))
- 删除所有值
try OYStore.removeAll(of: .temporary)
OYStore 基于 MIT 许可证发布。有关详细信息,请参阅LICENSE。