I have a class say,
class GroupClass{
var groupId: String = ""
var groupName: String = ""
}
I want to be able to store its object in NSUserDefaults
. I tried this way,
let group = GroupClass()
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(group, forKey: "group")
defaults.synchronize()
and retrieved it following way,
let defaults = NSUserDefaults.standardUserDefaults()
var group = defaults.objectForKey("group") as! GroupClass
It threw exception Using non-property obect
and crashed. What is the right way to do it in Swift?
Also tried the following way,
class GroupClass{
var groupId: String = ""
var groupName: String = ""
required init(coder aDecoder: NSCoder) {
self.groupId = aDecoder.decodeObjectForKey("groupId") as! String
self.groupName = aDecoder.decodeObjectForKey("groupName") as! String
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(self.groupId, forKey: "groupId")
aCoder.encodeObject(self.groupName, forKey: "groupName")
}
}
But, let group = GroupClass(coder:NSCoder())
gives problem.