Swift save cutom class in settings exception: 'NSInvalidArgumentException', reason: '*** -length only defined for abstract class -
hello try store custom object nsuserdefaults. used helpful post saving custom swift class nscoding userdefaults
but following exception when try out
2015-05-12 11:05:14.994 kirchnertime[4424:75095] * terminating app due uncaught exception 'nsinvalidargumentexception', reason: '* -length defined 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 { { return self._barcode } } public var projectnumber: string { { return self._projectnumber } set { self._projectnumber = newvalue } } public var projectdescription: string { { return self._projectdescription } set { self._projectdescription = newvalue } } public var jobdescription: string { { 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 object should stored.
public func savesettings() { let settings = nsuserdefaults.standarduserdefaults() settings.setvalue(self._cardnumber, forkeypath: cardnumberkey) settings.setobject(nskeyedarchiver.archiveddatawithrootobject(projectjobentity(barcode: "test")), forkey: "project") }
and how try it.
i had same exception.
i use nsobject parent class.
use under code
public class projectjobentity: nsobject { private let _barcode: string private var _projectnumber: string private var _projectdescription: string private var _jobdescription: string public var barcode: string { { return self._barcode } } public var projectnumber: string { { return self._projectnumber } set { self._projectnumber = newvalue } } public var projectdescription: string { { return self._projectdescription } set { self._projectdescription = newvalue } } public var jobdescription: string { { 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() } 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") } public func initwithcoder(adecoder: nscoder) -> projectjobentity { let barcode = adecoder.decodeobjectforkey("barcode") as! string let projectnumber = adecoder.decodeobjectforkey("projectnumber") as! string let projectdescription = adecoder.decodeobjectforkey("projectdescription") as! string let jobdescription = adecoder.decodeobjectforkey("jobdescription") as! string return projectjobentity(barcode: barcode, projectnumber: projectnumber, projectdescription: projectdescription, jobdescription: jobdescription) } }
Comments
Post a Comment