ios - Swift - Saving user data using NSKeyedArchiver, getting error on conversion to NSArray -
so have array of custom objects. when try save array nsuserdefaults, have archive them. here doing archive array of custom objects :
override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { //3 var allmessages = defaults.objectforkey(userdefaultsmesssagekey) as! [addmessageviewcontroller.harasstext] var amessage = addmessageviewcontroller.harasstext(phonenumber: 0, message: "1", frequency: 1, active: 1) allmessages.append(amessage) savemessage(allmessages) return (allmessages.count) } func archivemessage(message:[addmessageviewcontroller.harasstext]) -> nsdata { let archivedobject = nskeyedarchiver.archiveddatawithrootobject(message nsarray) return archivedobject } func savemessage(messages: [addmessageviewcontroller.harasstext]) { let archivedobject = archivemessage(messages) defaults.setobject(archivedobject, forkey: userdefaultsmesssagekey) defaults.synchronize() } func retrievedata()-> [addmessageviewcontroller.harasstext]? { if let unarchiveobject = nsuserdefaults.standarduserdefaults().objectforkey(userdefaultsmesssagekey) as? nsdata { return nskeyedunarchiver.unarchiveobjectwithdata(unarchiveobject) as? [addmessageviewcontroller.harasstext] } return nil }
the flow :
on savemessage call, pass custom array in archived. here array looks gdb. can see array, , has 1 class object contained in it.
([harass_your_kate.addmessageviewcontroller.harasstext]) $r0 = 1 value { [0] = 0x00007fa5ab409600 { objectivec.nsobject = { isa = harass_your_kate.addmessageviewcontroller.harasstext } phonenumber = 0 message = "1" frequency = 1 active = 1 } }
the array gets passed archivemessage. code fails on nskeyedarchiver method. error message follows :
[_ttcc16harass_your_kate24addmessageviewcontroller10harasstext encodewithcoder:]: unrecognized selector sent instance 0x7fa5ab409600
2016-07-15 16:31:26.019 harass kate[5858:7847495] * -[nskeyedarchiver dealloc]: warning: nskeyedarchiver deallocated without having had -finishencoding called on it. 2016-07-15 16:31:26.023 harass kate[5858:7847495] * terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[_ttcc16harass_your_kate24addmessageviewcontroller10harasstext encodewithcoder:]: unrecognized selector sent instance 0x7fa5ab409600
i appreciate of help, thank in advance!
edit: here definition of object:
class text : nsobject { var phonenumber = 0 //text address send var message: string = "" //message sent var frequency = 24 //number of hours between messages (usually multiple of 24 - 24 = daily) var active = 0 // 0 if cancelled, 1 if active init(phonenumber: int, message: string, frequency: int, active: int ){ self.phonenumber = phonenumber self.message = message self.frequency = frequency self.active = active } }
nskeyedarchiver
serializes objects taking advantage of methods you have provide conforming nscoding
protocol. can't automatically infer how save custom classes; have implement follows:
class text : nsobject, nscoding { var phonenumber = 0 //text address send var message: string = "" //message sent var frequency = 24 //number of hours between messages (usually multiple of 24 - 24 = daily) var active = 0 // 0 if cancelled, 1 if active init(phonenumber: int, message: string, frequency: int, active: int ){ self.phonenumber = phonenumber self.message = message self.frequency = frequency self.active = active } required init?(coder adecoder: nscoder) { phonenumber = adecoder.decodeinteger(forkey: "phonenumber") message = adecoder.decodeobject(forkey: "message") as! string frequency = adecoder.decodeinteger(forkey: "frequency") active = adecoder.decodeinteger(forkey: "active") } func encode(with acoder: nscoder) { acoder.encode(phonenumber, forkey: "phonenumber") acoder.encode(message, forkey: "message") acoder.encode(frequency, forkey: "frequency") acoder.encode(active, forkey: "active") } }
as can see, initializer reads coder , creates , instance, whereas encode
method saves relevant information coder. in simple class this, implementations may seem obvious, in more complex custom type, there non-trivial encoding decisions make.
Comments
Post a Comment