I am trying to save a custom object in the NSUserDefaults but it is retuing nill. I made sure that the object had values before it saves.
func syncObjects(){
let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setValue(NSKeyedArchiver.archivedDataWithRootObject(profile), forKey: "Profile")
userDefaults.setValue(NSKeyedArchiver.archivedDataWithRootObject(preference), forKey: "Preference")
userDefaults.synchronize()
print("objects synced")
}
func getObjects(){
if let data = NSUserDefaults.standardUserDefaults().objectForKey("Profile") as? NSData {
profile = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! UserProfile
if profile.firstName == nil {
print("Profile object reset in app delegate")
profile.reset()
}
}
if let data = NSUserDefaults.standardUserDefaults().objectForKey("Preference") as? NSData {
preference = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! UserPreference
}
print("retrieved objects")
}
In the custom object controller
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.theID forKey:@"theID"];
[aCoder encodeObject:self.fbID forKey:@"fbID"];
[aCoder encodeObject:self.email forKey:@"email"];
}
-(id)initWithCoder:(NSCoder *)aDecoder{
if(self = [super init]){
self.theID = [aDecoder decodeObjectForKey:@"theID"];
self.fbID = [aDecoder decodeObjectForKey:@"fbID"];
self.email = [aDecoder decodeObjectForKey:@"email"];
}
retu self;
}
