ios - 如何在 userDefaults swift 中进行循环

标签 ios iphone swift xcode

我正在使用 2 个文本字段作为 String、String 将数据保存到 userDeafults 中,但我想知道如何使用循环或函数检索所有要显示的数据

    let save = UserDefaults.standard

    let heading = headingText.text
    let description = desxriptionTexr.text
    save.set(description, forKey: heading!)

最佳答案

要获取 UserDefaults 中的所有键和对应的值,您可以使用:

for (key, value) in UserDefaults.standard.dictionaryRepresentation() {
            print("\(key) = \(value) \n")
}

swift 3 中,您可以使用以下方式存储和检索:

UserDefaults.standard.setValue(token, forKey: "user_auth_token")
print("\(UserDefaults.standard.value(forKey: "user_auth_token")!)")

关于ios - 如何在 userDefaults swift 中进行循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44945465/

相关文章:

ios - CollectionViewCell 未创建

ios - UIViewControllers 中带有许多 UITableVIew 的 UIScrollView 消失了 UITableViewCell 的点击

ios - 使用github操作时如何在faSTLane中选择xcode版本

iphone - 如果有nib文件会调用loadView吗?

iOS 8 "Copy/Paste"对话消失

iphone - 将 nsnumber 或 nsstring 放入 initWithLatitude :

json - Swift 中的 Realm DB 和对象映射器 (Json)

objective-c - typedef 枚举到 Swift 枚举。如何使用案例来标记idleTimerDisabled检测的Pause和Resume?

iOS 模拟器在 XCode 中颠倒加载

swift - 带有货币符号的自定义数字格式化程序