swift - 在 UserDefaults 中存储多个数组
问题描述
想知道是否可以在 iOS UserDefaults 存储中存储一个数组。我有一个带有不同配置文件的屏幕,将在订购过程中用作预定义的输入。没有用户登录系统,因此我计划为此使用 UserDefaults 模块而不是在线数据库存储。
所需数组:
[
"Profile 1" : ["title" : "Ing.",
"salutation" : "Herr",
"fullname" : "Franz Bernstein Stiglitz"
"phone" : "13001300",
"email" : "john2doe@example.com",
],
"Profile 2" : ["title" : "Dr.",
"salutation" : "Herr",
"fullname" : "Joseph Keusch"
"phone" : "06606666800",
"email" : "jkeusch_1974@example.com",
],
]
我有这个带有模型的 Struct 并使用它可以存储为单个 KVS(没有“数组逻辑”)。
struct OrderingProfiles {
static let (titleKey, salutationKey, fullnameKey, phoneKey, emailKey) = ("title", "salutation", "fullname", "phone", "email")
static let userSessionKey = "com.save.usersession"
struct OrderingProfile {
var title: String?
var salutation: String?
var fullname: String?
var phone: String?
var email: String?
init(_ json: [String: String]) {
self.title = json[titleKey]
self.salutation = json[salutationKey]
self.fullname = json[fullnameKey]
self.phone = json[phoneKey]
self.email = json[emailKey]
}
}
static var saveOrderingProfile = { (title: String, salutation: String, fullname: String, phone: String, email: String) in
UserDefaults.standard.set([titleKey: title, salutationKey: salutation, fullnameKey: fullname, phoneKey: phone, emailKey: email], forKey: userSessionKey)
}
static var getOrderingProfile = { _ -> OrderingProfile in
return OrderingProfile((UserDefaults.standard.value(forKey: userSessionKey) as? [String: String]) ?? [:])
}(())
static func clearUserData(){
UserDefaults.standard.removeObject(forKey: userSessionKey)
}
}
解决方案
如果你的回复是这样的
[
"Profile" : ["title" : "Ing.",
"salutation" : "Herr",
"fullname" : "Franz Bernstein Stiglitz"
"phone" : "13001300",
"email" : "john2doe@example.com",
],
"Profile" : ["title" : "Dr.",
"salutation" : "Herr",
"fullname" : "Joseph Keusch"
"phone" : "06606666800",
"email" : "jkeusch_1974@example.com",
],
]
使用第三方模型映射
库链接尝试此代码:- https://github.com/tristanhimmelman/ObjectMapper
1) 模型类
import UIKit
import ObjectMapper
class ProfileModel:NSObject , Mappable , NSCoding {
var Profile:[ProfileData]?
required init?(map: Map) {
}
required init(coder aDecoder: NSCoder) {
self.Profile = aDecoder.decodeObject(forKey:"Profile") as? [ProfileData]
}
public func encode(with aCoder: NSCoder) {
aCoder.encode(Profile, forKey: "Profile")
}
func mapping(map: Map) {
Profile <- map["Profile"]
}
}
class ProfileData:NSObject , Mappable , NSCoding {
var title:String?
var salutation:String?
var fullname:String?
var phone:String?
var fullname:String?
required init?(map: Map) {
}
//==========================================
//MARK: - NSCoder Initializer Methods
//==========================================
required init(coder aDecoder: NSCoder) {
self.title = aDecoder.decodeObject(forKey: "title") as? String
self.salutation = aDecoder.decodeObject(forKey:"salutation") as? String
self.fullname = aDecoder.decodeObject(forKey:"fullname") as? String
self.phone = aDecoder.decodeObject(forKey:"phone") as? String
self.email = aDecoder.decodeObject(forKey:"email") as? String
}
public func encode(with aCoder: NSCoder) {
aCoder.encode(title, forKey: "title")
aCoder.encode(salutation, forKey: "salutation")
aCoder.encode(fullname, forKey: "fullname")
aCoder.encode(phone, forKey: "phone")
aCoder.encode(email, forKey: "email")
}
//==========================================
//MARK: - Mappable Protocol Methods
//==========================================
func mapping(map: Map) {
title <- map["title"]
salutation <- map["salutation"]
fullname <- map["fullname"]
phone <- map["phone"]
email <- map["email"]
}
}
在 UserDefaults 中保存数据
1) 保存数据代码
var arrayProfile:[ProfileData] = []
var arrprofile:[ProfileModel] = []
let defaults = UserDefaults.standard
for data in arrprofile
{
arrayProfile.append(data)
}
let saveProfileData = NSKeyedArchiver.archivedData(withRootObject: arrayProfile)
defaults.set(saveProfileData, forKey: "profile")
defaults.synchronize()
2) 检索数据
var arrayProfile:[ProfileData] = []
let defaults = UserDefaults.standard
guard let profile_data = defaults.object(forKey: "profile") as? NSData else {
print("'profile' not found in UserDefaults")
return
}
guard let placesArray = NSKeyedUnarchiver.unarchiveObject(with: profile_data as Data) as? [ProfileData] else {
print("Could not unarchive from profile_data")
return
}
for place in placesArray
{
arrayProfile.append(place)
}
在我的情况下工作 100% 试试这个
推荐阅读
- javascript - 使用下一个和上一个图像箭头进行导航的 JS/jQuery 数组循环缩略图
- kotlin - Kotlin 通用设置函数
- git - git用vscode“无法将对象迁移到永久存储”是什么意思
- sql - SQL 从所有列都等于值的表中选择
- vb.net - 将 datagridview 值传递给带有 vb.net 格式的消息框
- javascript - 点击功能无法与 codeigniter anchor() 一起正常工作
- react-native - 将图像从 React Native 上传到 LoopBack
- java - Eclipse - 提取具有相似变量名的常量时的错误建议
- machine-learning - 为什么我不能直接使用特征矩阵进行预测?
- mongodb - mongodb中单个查询的多个计数