Lets say i have a class called Lop
. then i would like to save instances of it to user defaults in some array, and also retrieve objects from this array .
I have created the NSCoding
method in that class and now trying to write functions to save/retrieve and having hard time :
to add for example :
func addLop(newlop:ALop)
{
//get defaults array(already archived)
let lops = NSUserDefaults.standardUserDefaults().objectForKey("lops")
//add a new lop class, archived, to the user defaults array
let data = NSKeyedArchiver.archivedDataWithRootObject(newlop)
lops?.addObject(data)
NSUserDefaults.standardUserDefaults().setObject(lops, forKey: "lops")
}
Which i don't really know if works because the retrieve function I just couldn't handle to write :
func getLops()->Array<Any>
{
//get defaults array(archived)
let lops = NSUserDefaults.standardUserDefaults().objectForKey("lops")
//here i should loop over this array of archived classes ,
then turn all of them into original unarchived classes and return
}
1.How can I write a function that returns the array of unarchived classes ?
- when saving (if done right) how can I check if a class already exist in user defaults before saving?