当前位置:网站首页>为 @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/
边栏推荐
- [ecmascript6] modularization
- Raspberry pie foundation | summarize and record some operations in the learning process of raspberry pie
- Hcip day 12
- 58子站安居,经纪人营销管理平台登录接口加密逆向
- 2022 melting welding and thermal cutting examination questions and online simulation examination
- Leetcode 0143. rearrange linked list
- [ecmascript6] async and await
- C # read INI file and key value pair operation
- Cv:: mat conversion to qimage error
- Thrift 序列化协议浅析
猜你喜欢

58子站安居,经纪人营销管理平台登录接口加密逆向

Leetcode 0142. circular linked list II

Leetcode 0143. rearrange linked list

Copy excel row to specified row

如何有效进行回顾会议(上)?

2022 safety officer-a certificate operation certificate examination question bank simulated examination platform operation

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

HCIP第十天

Forage QR code -- online QR code generator
C# 获取当前路径7种方法
随机推荐
如何让照片中的人物笑起来?HMS Core视频编辑服务一键微笑功能,让人物笑容更自然
It's so hot that solar power can't take off? Hello, head
【七夕】七夕孤寡小青蛙究极版?七夕节最终章!
OKR and grad
2022 melting welding and thermal cutting examination questions and online simulation examination
如何有效进行回顾会议(上)?
国产数据库的红利还能“吃”多久?
Realization of chat room function
Collaborative office tools: Online whiteboard is in its infancy, and online design has become a red sea
2022高处安装、维护、拆除考试题库及在线模拟考试
How does vos3000 send incoming calls to okcc
C# 读取ini文件、键值对操作
@Solution to DS ('slave') multi data source compatible transaction problem
[ecmascript6] proxy and reflection
实时切换 Core Data 的云同步状态
[ecmascript6] class
Pointers and arrays (7)
AFNetworking速成教程
IP black and white list
Tdengine helps Siemens' lightweight digital solutions