ios - Can't insert object in the array, that I want add to User Defaults -


i have initialization of array

    var defaults = nsuserdefaults.standarduserdefaults()     var initobject: [cpaymentinfo]? = defaults.objectforkey("paymentdata") as? [cpaymentinfo]     if initobject == nil {         initobject = [cpaymentinfo]()         defaults.setvalue(initobject, forkey: "paymentdata")     }     defaults.synchronize() 

and have controller, contains 2 text labels , 2 buttons: save , cancel (both calling segue)

    if sender as? uibarbuttonitem == savebutton {         var defaults = nsuserdefaults.standarduserdefaults()         var payments: [cpaymentinfo]? = defaults.objectforkey("paymentdata") as? [cpaymentinfo]         var newpaymentitem: cpaymentinfo?         if discriptionfield.hastext() {             newpaymentitem = cpaymentinfo(value: (valuefield.text nsstring).doublevalue, discription: discriptionfield.text)         } else {             newpaymentitem = cpaymentinfo(value: (valuefield.text nsstring).doublevalue)         }          if let newpay = newpaymentitem {             payments?.insert(newpay, atindex: 0)         }         defaults.setobject(payments, forkey: "paymentdata")     } 

but doesn't work , app crashes. found can set "payments" new object in defaults key without changing (by commenting of block insert). found, can comment line setobject method , apps work too. how can want?


2015-07-14 20:34:34.403 cash app[15156:1494701] property list invalid format: 200 (property lists cannot contain objects of type 'cftype') 2015-07-14 20:34:34.404 cash app[15156:1494701] attempt set non-property-list object ( "cash_app.cpaymentinfo" ) nsuserdefaults/cfpreferences value key paymentdata 2015-07-14 20:34:34.409 cash app[15156:1494701] *** terminating app due uncaught exception 'nsinvalidargumentexception', reason: 'attempt insert non-property list object ( "cash_app.cpaymentinfo" ) key paymentdata'

the answer in error message:

attempt insert non-property list object ( "cash_app.cpaymentinfo" ) key paymentdata' 

you cannot put arbitrary classes property list (such nsuserdefaults). there specific list of classes acceptable. if have other classes want store, need convert them acceptable class.

for documentation on saving non-property-list classes user defaults, see storing nscolor in userdefaults. while article focuses on nscolor, applies object conforms nscoding. see note @ introduction property lists.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -