Can NSUserDefaults contain an NSArray with custom objects?

The name pretty much explains this. Do I need to serialize objects first, or is this possible?

+5
source share
4 answers

You must encode / decode the objects in your object (which is in your array) and archive the array in NSData.

Just add

<NSCoding>

into the class of your objects (in your array) and follow the warnings of your compiler: D

Then archive your array as follows:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:yourArray];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:data forKey:@"yourKey"];


NSArray *array= [NSKeyedUnarchiver unarchiveObjectWithData:[defaults objectForKey:@"yourKey"];

Check this out http://soff.es/archiving-objective-c-objects-with-nscoding

+11
source

You need to serialize it.

From the Apple documentation for NSUSerDefaults:

NSUserDefaults , float, doubleles, integers, Booleans URL. , ( - ): NSData, NSString, NSNumber, NSDate, NSArray NSDictionary. - , , , NSData. . .

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html

+1

yes, maybe you just need to take care of the NULL values ​​when storing and retrieving them,

+1
source

All Articles