Ios 使用UserDefault存储和检索数组数据
在我的场景中,我需要将Ios 使用UserDefault存储和检索数组数据,ios,swift,nsuserdefaults,Ios,Swift,Nsuserdefaults,在我的场景中,我需要将array值存储到UserDefault中并检索相同的数据。当用户再次打开特定的viewcontroller时,检索数据需要加载到相同的数组中。下面是我正在尝试的代码。我不知道如何将数组输出值下的存储和检索以正确的方式存储到UserDefaults中。请帮我做这个 将数据存储到数组中 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { self.tab
array
值存储到UserDefault
中并检索相同的数据。当用户再次打开特定的viewcontroller
时,检索数据需要加载到相同的数组中。下面是我正在尝试的代码。我不知道如何将数组输出值下的存储
和检索
以正确的方式存储到UserDefaults中。请帮我做这个
将数据存储到数组中
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tableView.deselectRow(at: indexPath, animated: true)
let item = searching ? filteredData[indexPath.row] : membersData[indexPath.row]
if let cell = tableView.cellForRow(at: indexPath) {
if cell.accessoryType == .checkmark {
cell.accessoryType = .none
// UnCheckmark cell JSON data Remove from array
self.selectedRows = selectedRows.filter{$0 != indexPath.row}
self.selectedValues.remove(item) // Here Data Removing Into Array
} else {
cell.accessoryType = .checkmark
// Checkmark selected data Insert into array
self.selectedRows.append(indexPath.row) //select
self.selectedValues.insert(item) // Here Data Storing Into Array
}
}
}
将数组数据保存到用户默认值中
@IBAction func doneAction(_ sender: Any) {
// Selected Row Index Store
UserDefaults.standard.set(selectedRows, forKey: "SelectedIndexes")
// Here need to store Selected values
self.dismiss(animated: true, completion: nil)
}
将用户默认存储的数据重新加载到同一数组中(viewDidLoad)
可编码的
// MARK: - ListData
struct ListData: Codable, Hashable {
let userid: String?
let firstname, designation: String?
let profileimage: String?
var isSelected = false
private enum CodingKeys : String, CodingKey {
case userid, firstname, designation, profileimage
}
}
阵列数据
选择值:[ListData(用户标识:可选(“1”),名字:
可选(“abc”),名称:可选(“英文”),外形图:
可选(“url”)、列表数据(用户ID:
可选(“2”),名字:可选(“def”),名称:
可选(“数字”)配置文件图像:
可选(“url”)]选择行:[0,1]
如果您想存储在UserDefault中,尽管不建议这样做
var selectedValues: [Any] = [["id": 1, "name": "Adam"]]
selectedValues.append(["id": 2, "name": "Eve"])
UserDefaults.standard.set(selectedValues, forKey: "SelectedValues")
let result = UserDefaults.standard.array(forKey: "SelectedValues") ?? []
print(result)
Output : [{
id = 1;
name = Adam;
}, {
id = 2;
name = Eve;
}]
如果要在UserDefaults中保存列表,则只能在UserDefaults中保存
Any
数据类型列表。首先,您应该将所选行保存在任何
数据类型列表中。步骤1:将列表声明为
Any
数据类型,并在其中附加选定的行数据
var selectedValues: [Any]?
第2步:在单独的类中创建一个变量,您的所有用户默认值都将该存储Any
数据类型列表保存在
//MARK: store selectedValues
private let KeySelectedValues = "KeySelectedIndexes"
fileprivate var _selectedValueList = [Any]()
var selectedValueList : [Any] {
get {
_selectedValueList = UserDefaults.standard.object(forKey: KeySelectedIndexes) as? [Any] ?? _selectedValueList
return _selectedValueList
}
set {
_selectedValueList = newValue
UserDefaults.standard.set(_selectedValueList, forKey: KeySelectedIndexes)
}
}
第3步:将列表保存在UserDefaults中,如
classObj.selectedValueList = selectedValues
步骤4:从UserDefaults检索数据列表
let _selectedValueList = classObj.selectedValueList
guard let selectedValueList:[YourSelectedRowModel] = Mapper<YourSelectedRowModel>().mapArray(JSONArray: _selectedValueList as! [[String : Any]]) else {
return
}
让_selectedValueList=classObj.selectedValueList
guard让selectedValueList:[YourSelectedRowModel]=Mapper().mapArray(JSONArray:_SelectedValueListAs![[String:Any]]其他{
返回
}
为什么任何对象
?所有值都是值类型。为什么value(forKey
)?有array(forKey
@vadian是的,你是对的。我已经更新了代码,你可以查看并告诉我它是否有效。你只能UserDefaults中任何有错误的数据类型列表。你可以将所有符合属性列表的同质类型保存为UserDefaults中的数组([String],[Int]
,[Double]
,[Bool]
,[Date]
,[Data]
)。@vadian我说了任何数据类型列表。你不能保存任何其他模型类型列表。数据类型列表是什么意思?代码显示了一个数组。你可以保存[Int]
–它实际上代表的是selectedRows
,但如果您要保存不符合属性列表的类型,您的代码将崩溃。selectedRows是一个数据类型为a模型的列表或数组,该模型包含多个类型变量,如int、string、bool等。您可以保存与[int]、[string]相同的数组数据类型,但不能保存[YourModelType]。因此,我的意思是,如果您想在UserDefault中保存模型类型列表或数组,可以借助我的代码进行保存。不,问题中的selectedRows
显然是[Int]
。评论不用于扩展讨论;此对话已被删除。@PGDev搜索后搜索结果复选标记不工作,但没有搜索复选标记工作正常。
let _selectedValueList = classObj.selectedValueList
guard let selectedValueList:[YourSelectedRowModel] = Mapper<YourSelectedRowModel>().mapArray(JSONArray: _selectedValueList as! [[String : Any]]) else {
return
}