问题
In my scenario, I am storing
array data into UserDefault
in VC2
. In another VC1
I am retrieving
data. Now, I need to load data into collectionview
array in VC1. Here, I am using multiple datasource Instead of that want to use single data source to get a specific values to load into collectionview. How to achieve this?
NOTE: while dismissing VC2
I need to pass the array value to VC1
Array Data Storing Into UserDefault in VC2
let selectedData = membersData.filter{$0.isSelected}
UserDefaults.standard.set(try? PropertyListEncoder().encode(selectedData), forKey:"sessiondata")
Stored Data Retrieving In VC1
func storeValidaion(){
// Retrive Array Values
if let data = UserDefaults.standard.value(forKey:"sessiondata") as? Data {
let sessionData = try? PropertyListDecoder().decode(Array<TeamListData>.self, from: data)
print("ARRAY VALUES: \(sessionData!)")
// here how to load array data into collection view
if sessionData?.isEmpty == false {
for person in sessionData! {
print(person.userid!)
if useridArray.contains(person.userid!) {
print("yes")
} else {
self.useridArray.append(person.userid!)
self.nameArray.append(person.firstname!)
self.profileArray.append(person.profileimage!)
print(useridArray)
print(profileArray)
}
self.collectionView.reloadData()
}
} else {
UserDefaults.standard.removeObject(forKey:"sessiondata")
self.useridArray.removeAll()
self.profileArray.removeAll()
self.nameArray.removeAll()
self.collectionView.reloadData()
}
}
}
/*ARRAY VALUES: [ListData(userid: Optional("1"), firstname: Optional("abc"), designation: Optional("student"), profileimage: Optional("url"), isSelected: false), ListData(userid: Optional("2"), firstname: Optional("def"), designation: Optional("Executive"), profileimage: Optional("url"), isSelected: false)]*/
回答1:
Below code inside VC2
Array Data Storing Into UserDefault in VC2
let selectedData = membersData.filter{$0.isSelected}
UserDefaults.standard.set(try? PropertyListEncoder().encode(selectedData), forKey:"sessiondata")
adopt VC2Delegate
func dismissProtocole(data: Any) {
<#code#>
}
Present VC2 inside VC1
if let details = self.storyboard?.instantiateViewController(withIdentifier: "VC2") as?VC2 {
details.modalPresentationStyle = .fullScreen
details.modalTransitionStyle = .crossDissolve
**details.selectedData = selectedData** // pass data
details.delegate = self
self.navigationController?.present(details, animated: true, completion: nil)
}
Inside VC2
var selectedData : [TeamListData]? // instance method var delegate : VC2Delegate?
Stored Data Retrieving In VC2
func storeValidaion(){
// Retrive Array Values
if selectedData == nil {
if let data = UserDefaults.standard.value(forKey:"sessiondata") as? Data {
guard let sessionData = try? PropertyListDecoder().decode(Array<TeamListData>.self, from: data) else {
return
}
print("ARRAY VALUES: \(sessionData)")
self.selectedData = sessionData
self.collectionView.reloadData()
// put your code whatever you want....
}
}
}
Dismiss method
func dismiss() {
self.delegate?.dismissProtocole(data: sessionData)
}
Protocol declaration
protocol VC2Delegate {
func dismissProtocole( data : Any)
}
回答2:
Save Data with Archive and fetch data with Unarchive in UserDefaults
func saveCustomData() throws {
let array = ["New","Old"]
let encodedData = try NSKeyedArchiver.archivedData(withRootObject: array, requiringSecureCoding: false)
UserDefaults.standard.set(encodedData, forKey: "data")
}
func retrieveData() throws {
if let data = UserDefaults.standard.data(forKey: "data"),
let dataArr = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? [String] {
} else {
print("There is an issue")
self.saveCustomData()
}
}
来源:https://stackoverflow.com/questions/58730083/swift-userdefault-stored-array-data-load-into-uicollectionview