Unable to save array to NSUserDefaults -
this first attempt @ using nsuserdefaults. i've read every question & answer posted in stackoverflow regarding subject, still can't work. must missing basic. array (allcontacts) merely contains few names , phone numbers. unless i'm misunderstanding what's happening, both fields nsstrings. or pointers strings? if that's case, how convert them actual nsstrings? here's code save array:
- (bool)savechanges { nsmutablearray *mutablearray = [[nsmutablearray alloc] initwitharray:allcontacts]; [[nsuserdefaults standarduserdefaults] setobject:mutablearray forkey:@"allcontacts"]; [[nsuserdefaults standarduserdefaults] synchronize]; return 1; }
here log:
2013-08-07 15:48:17.568 imok[5515:907] *** -[nsuserdefaults setobject:forkey:]: attempt insert non-property value '( "brad pitt, 1-917-297-1234", "marilyn monroe, 9179291234" )' of class '__nsarraym'. note dictionaries , arrays in property lists must contain property values.
thanks in advance help.
this code "for(id contact in allcontacts) { nslog(@"%@", [contact class]); }" gives me:
2013-08-07 17:11:30.845 imok[5569:907] contacts 2013-08-07 17:11:30.854 imok[5569:907] contacts
so guess not valid property values. incidentally, there no intelligent reason me attempt save array nsmutablearray. experimenting.
from apple documentation: https://developer.apple.com/library/mac/#documentation/cocoa/reference/foundation/classes/nsuserdefaults_class/reference/reference.html#//apple_ref/doc/c_ref/nsuserdefaults
a default object must property list, is, instance of (or collections combination of instances of): nsdata, nsstring, nsnumber, nsdate, nsarray, or nsdictionary. if want store other type of object, should typically archive create instance of nsdata.
if allcontacts
nsarray
, use in setobject:forkey:
call. if it's nsmutablearray
, use:
[allcontacts copy]
to non-mutable copy of array.
Comments
Post a Comment