in class firstviewcontroller
, add array key renteditems
nsuserdefaults in following lines:
let itemarray = [item]() let prefs:nsuserdefaults = nsuserdefaults.standarduserdefaults() prefs.setobject(itemarray, forkey: "renteditems")
then, in class secondviewcontroller
, try
var item: item? var prefs:nsuserdefaults = nsuserdefaults.standarduserdefaults() func confirmpressed() { prefs.arrayforkey("renteditems")?.append(item) }
this gives following error: cannot use mutating member on immutable value: function call returns immutable value
. saw solution in here gave me idea of problem, nsuserdefaults being default ios class, can't use same solution there. ideas?
you can't directly modify array defaults. instead make copy, , modify that. can save array defaults afterwards. try this:
var array = prefs.arrayforkey("renteditems")?.mutablecopy array.append(item)
Comments
Post a Comment