惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Register - Security
The Register - Security
The Cloudflare Blog
Last Week in AI
Last Week in AI
月光博客
月光博客
Security Latest
Security Latest
Project Zero
Project Zero
博客园 - 司徒正美
AWS News Blog
AWS News Blog
雷峰网
雷峰网
博客园 - 聂微东
C
Cybersecurity and Infrastructure Security Agency CISA
P
Palo Alto Networks Blog
人人都是产品经理
人人都是产品经理
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tor Project blog
Martin Fowler
Martin Fowler
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
A
About on SuperTechFans
V
V2EX
P
Proofpoint News Feed
腾讯CDC
S
Schneier on Security
K
Kaspersky official blog
Google Online Security Blog
Google Online Security Blog
Recorded Future
Recorded Future
H
Hacker News: Front Page
Application and Cybersecurity Blog
Application and Cybersecurity Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
C
CERT Recently Published Vulnerability Notes
aimingoo的专栏
aimingoo的专栏
博客园 - 叶小钗
美团技术团队
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
Vulnerabilities – Threatpost
C
Cisco Blogs
PCI Perspectives
PCI Perspectives
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 【当耐特】
N
News and Events Feed by Topic
Google DeepMind News
Google DeepMind News
Hugging Face - Blog
Hugging Face - Blog
阮一峰的网络日志
阮一峰的网络日志
P
Privacy International News Feed
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
W
WeLiveSecurity
Spread Privacy
Spread Privacy
S
Securelist

木土金王可

复盘 2024,年度好物分享 落地 第二十台相机:宝丽来(Polaroid)Impulse AF 第十九台相机:哈苏(Hasselblad)X1D II 第十八台相机:宝丽来(Polaroid)Macro 5 SLR 第十七台相机:康泰时(Contax)S2 稻荷山 Dave Chappelle: The Closer 语言是思想的边界 对街头卖惨表演的看法 《民主森林》 电梯英雄 2023,年度好物分享 《大画幅 4x5》复活 新加坡华侨银行(OCBC)开户指南 第十六台相机:宾得(Pentax)PC35AF-M SEDATE 第十五台相机:沙慕尼(Chamonix)4x5 F2 Xbox 连接 Studio Display Twitter、Apple、Meta 和 Reddit 广告平台使用体验 第十四台相机:康泰时(Contax)S2 你到底什么时候买相机 拍摄胶片的理由 HomePod 不能作为 Mac 音响 男性生来就是残缺的女性 第十三台相机:柯尼卡(Konica)Big Mini 301 三年,十二台相机 恐怖片很好看啊 比残忍更残忍 用 SwiftUI 重构「行动日」 推荐《女生呵护指南》 你是左派还是右派? 驯服内心的猛犸象:为什么你应该停止关心别人对你的想法 想写点什么,但又不知道该写什么 摄影构图 精致的结婚照 中文没死,是有的人死了 上海爆发 COVID-19 疫情以来我收藏的文章(持续更新) 自我审查 25 美元,成为美国计算机学会(ACM)会员 将同事送我的仙人掌转换成 3D 模型 当我们谈论离屏渲染时,我们在谈论什么 如何让 iOS 给你的应用开后门 二零一九年夏秋之交工作生活的异动 一声一世 iOS 限制输入字数与特殊字符的巧妙方法 对于番茄学习法的误解 Swift 中的空字符串 我如何自学 iOS 开发 为什么我不喜欢社交网络 如何与人见面 为什么我不喜欢即时通讯 在文件沙盒中读取与写入应用数据 在 iOS 开发中使用键值对监听(KVO)与通知(Notification) 介绍 GCD 中的线程,队列,延迟执行,线程阻断,信号量,任务组与任务对象 iOS 原生 URLSession 网络请求 什么是自动引用计数(ARC)和引用循环 在 iOS 开发中使用代理与协议 独立开发的第一个应用:行动日 为什么我会把 Google Pixel 换成 iPhone SE? 手动进行 Nexus 和 Pixel 系列手机的 OTA 更新 开启 Chromebook 的夜间模式
以配置信息(UserDefaults)的形式存储应用数据
kinnouka bokudo · 2018-09-10 · via 木土金王可

存储配置信息,可以使用 UserDefaults,这是一种最简单的储存数据的方法,适合用来储存轻量级的数据,UserDefaults 支持储存的数据类型只有 DataStringNumberDateArrayDictionary 这几种。

声明:笔者自身对 UserDefaults 也是初学,而本文介绍的 UserDefaults 知识也都是自己的一些理解,同时尽量不涉及过于原理性的内容,一切以普通工程师实用为目标原则。其中可以想象在很多地方会有理解的错误,还请多包涵。如您发现问题,也往不吝赐教指正,感激不尽。

储存

储存值为「Hello」的字符串:

UserDefaults.standard.set("Hello", forKey: "helloString")

而如果准备将储存的数据在主程序(Container)和其他扩展(Extensions)之间传递,则需要在 UserDefaults 中添加 Group 字符串:

UserDefaults.init(suiteName: "group.com.xxx.xxx")?.set("hello", forKey: "helloString")

加载

加载 Key 值为「helloString」的数据:

let helloStr = UserDefaulte.standard.object(forKey: "helloString")

print(helloStr)
// Output: Hello

同样在其他扩展(Extensions)中,也需要添加 Group 字符串,才能加载相应数据。

let helloString = UserDefaults.init(suiteName: "group.com.xxx.xxx")?.value(forKey: "helloString")

print(helloStr)
// Output: Hello

2018 年 9 月 10 日