当前位置:网站首页>为 @CloudStorage 添加了类 @Published 的能力
为 @CloudStorage 添加了类 @Published 的能力
2022-07-28 13:46:00 【东坡肘子】
我重新修改了 CloudStorage 的代码,现在可以同 AppStorage 完全一样将 UserDefaults 和 NSUbiquitousKeyValueStore 汇总到一起,在 SwiftUI 中统一管理了。
下周我们将聊聊它的实现原理 —— 【如何为属性包装器添加类 Published 的能力】
class Settings:ObservableObject {
@AppStorage("name") var name = "fat"
@AppStorage("age") var age = 5
@CloudStorage("readyForAction") var readyForAction = false
@CloudStorage("speed") var speed: Double = 0
}
struct DemoView: View {
@StateObject var settings = Settings()
var body: some View {
Form {
TextField("Name",text: $settings.name)
TextField("Age", value: $settings.age, format: .number)
Toggle("Ready", isOn: $settings.readyForAction)
.toggleStyle(.switch)
TextField("Speed",value: $settings.speed,format: .number)
}
.frame(width: 400, height: 400)
}
}
CloudStorage 原作者尚未合并修改后的代码,目前可下载我的 Fork 版本[1]。
关于 NSUbiquitousKeyValueStore 请参阅 在 SwiftUI 下使用 NSUbiquitousKeyValueStore 同步数据[2]
关于 AppStorage 可以阅读 @AppStorage 研究[3]
因为微信公众号中的文章只能修改10个字符,因此只有博客上的文章 www.fatbobman.com 才会保持更新,望见谅。
参考资料
[1] Fork 版本: https://github.com/fatbobman/CloudStorage
[2] 在 SwiftUI 下使用 NSUbiquitousKeyValueStore 同步数据: https://www.fatbobman.com/posts/nsubiquitousKeyvalueStore/
[3] @AppStorage 研究: https://fatbobman.com/posts/appstorage/
边栏推荐
- Database optimization understanding these is enough
- [ecmascript6] set and map
- Nport serial server configuration website (whether the serial server is from network port to serial port)
- Node file operation
- [线程安全问题] 多线程到底可能会带来哪些风险?
- 如何在 Core Data 中进行批量操作
- What is gossip (E-Net gossip)
- Floating point data type in C language (did you learn to waste it)
- Summarize the knowledge points of the ten JVM modules. If you don't believe it, you still don't understand it
- @Solution to DS ('slave') multi data source compatible transaction problem
猜你喜欢

Open source project - taier1.2 release, new workflow, tenant binding simplification and other functions

如何只降3D相机不降UI相机的分辨率

MeterSphere--开源持续测试平台

@DS('slave') 多数据源兼容事务问题解决方案

Many "double first-class" universities have launched the research guarantee and prediction name!

HCIP第十二天

Leetcode 0142. circular linked list II

2022年安全员-A证操作证考试题库模拟考试平台操作

这3款在线PS工具,得试试

2022 high altitude installation, maintenance, removal of examination question bank and online simulated examination
随机推荐
一些企业数据平台建设的思考
实时切换 Core Data 的云同步状态
Literature reading (245) roller
Career planning of Software Test Engineer
@DS('slave') 多数据源兼容事务问题解决方案
IP black and white list
7.27 simulation summary
朗镜科技(Trax中国)“机器人+AI”开启中国零售元宇宙时代
OKR与GRAD
Afnetworking crash course
2022高处安装、维护、拆除考试题库及在线模拟考试
Thoughts on the construction of some enterprise data platforms
Node file operation
如何在 Core Data 中进行批量操作
HCIP第十一天
C语言中浮点数据类型(你学废了吗)
zabbix分布式
Three methods to disassemble the rotation array
AFNetworking速成教程
爆肝整理JVM十大模块知识点总结,不信你还不懂