You can't store custom objects into user defaults. You can only store "property list objects" (NSString, NSData, NSDate, NSNumber, NSArray, or NSDictionary objects).
In order to save a custom object to user defaults you have to convert it to one of those types.
You can make your custom object conform to the NSCoding protocol. To do that you have to implement the init(coder:)
and encode(coder:)
methods. (I believe that's the swift form of the method names - I've been working in Objective-C lately so my Swift is getting rusty.) How you implement those methods depends on your class. Typically you make multiple calls to encodeObject:forKey:
to encode your object's properties.
Once your custom object conforms to NSCoding you can convert it to NSData using the NSKeyedArchiver
method archivedDataWithRootObject()
. You then convert it from NSData back to the original object using the NSKeyedUnarchiver
method unarchiveObjectWithData()
.
EDIT:
I just realized that you were trying to save a view controller object (SPTAuthViewController
to user defaults. You should not serialize and save view controller objects. Controller objects should not be used to save application data.