Hello I try to store a custom object with NSUserDefaults. I used this helpful post Saving custom SWIFT class with NSCoding to UserDefaults
But I get the following exception when I try it out
2015-05-12 11:05:14.994 KirchnerTime[4424:75095] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -length only defined for abstract class. Define -[Project length]!'
import Foundation
@objc(Project)
public class ProjectJobEntity: NSData, NSCoding
{
private let _barCode: String
private var _projectNumber: String
private var _projectDescription: String
private var _jobDescription: String
public var BarCode: String
{
get { return self._barCode }
}
public var ProjectNumber: String
{
get { return self._projectNumber }
set { self._projectNumber = newValue }
}
public var ProjectDescription: String
{
get { return self._projectDescription }
set { self._projectDescription = newValue }
}
public var JobDescription: String
{
get { return self._jobDescription }
set { self._jobDescription = newValue }
}
public init(barCode: String, projectNumber: String, projectDescription: String, jobDescription: String)
{
self._barCode = barCode
self._projectNumber = projectNumber
self._projectDescription = projectDescription
self._jobDescription = jobDescription
super.init()
}
public init(barCode: String)
{
self._barCode = barCode
self._projectNumber = ""
self._projectDescription = ""
self._jobDescription = ""
super.init()
}
override public func encodeWithCoder(aCoder: NSCoder)
{
aCoder.encodeObject(self._barCode, forKey: "barCode")
aCoder.encodeObject(self._projectNumber, forKey: "projectNumber")
aCoder.encodeObject(self._projectDescription, forKey: "projectDescription")
aCoder.encodeObject(self._jobDescription, forKey: "jobDescription")
}
required public init(coder aDecoder: NSCoder)
{
self._barCode = aDecoder.decodeObjectForKey("barCode") as! String
self._projectNumber = aDecoder.decodeObjectForKey("projectNumber") as! String
self._projectDescription = aDecoder.decodeObjectForKey("projectDescription") as! String
self._jobDescription = aDecoder.decodeObjectForKey("jobDescription") as! String
super.init(coder: aDecoder)
}
this is the object which should be stored.
public func saveSettings()
{
let settings = NSUserDefaults.standardUserDefaults()
settings.setValue(self._cardNumber, forKeyPath: cardNumberKey)
settings.setObject(NSKeyedArchiver.archivedDataWithRootObject(ProjectJobEntity(barCode: "Test")), forKey: "Project")
}
and this is how I try it.