Is it possible to save an integer array using NSUserDefaults on the iPhone? I have an array declared in my .h file as: int playfield[9][11]
that gets filled wi
Just use NSArray instead of normal C array then it will solve your problem easily.
You'll have to convert this to an object. You can use NSArray or NSDictionary.
From Apple's NSUserDefaults documentation:
A default’s value must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary.
This is why you are getting the pointer error.
You have several options (in order of recommended usage):
NSArray
of NSArray
s to store playField
in your application
int
, but fill an NSArray with numbers before saving to NSUserDefaults
.
NSArchiver
to convert between an array of integers and NSData
.
You can save and retrieve the array with a NSData wrapper
ie (w/o error handling)
Save
NSData *data = [NSData dataWithBytes:&playfield length:sizeof(playfield)];
[prefs setObject:data forKey:@"slot1Save"];
Load
NSData *data = [prefs objectForKey:@"slot1Save"];
memcpy(&playfield, data.bytes, data.length);