跳至主内容
版本:当前版本

设置

非官方测试版翻译

本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →

SettingsNSUserDefaults 的封装,后者是一个仅在 iOS 上可用的持久化键值存储。

示例


参考

方法

clearWatch()

tsx
static clearWatch(watchId: number);

watchId 是订阅最初配置时由 watchKeys() 返回的数字。


get()

tsx
static get(key: string): any;

获取 NSUserDefaults 中给定 key 的当前值。


set()

tsx
static set(settings: Record<string, any>);

NSUserDefaults 中设置一个或多个值。


watchKeys()

tsx
static watchKeys(keys: string | array<string>, callback: () => void): number;

订阅以在 NSUserDefaults 中由 keys 参数指定的任意键的值发生变化时收到通知。返回一个 watchId 数字,可用于 clearWatch() 来取消订阅。

备注

watchKeys() 按设计会忽略内部的 set() 调用,并且仅在 React Native 代码外部发生更改时触发回调。