devonsmith
devonsmith

Reputation: 123

store array of cllocation value in userdefaults iphone sdk

hi i have an array of cllocation values, i need to store that array in userdefaults...how do i do it...?
hi these where i add object to my array: [AM_locationMP.favArray addObject:[AM_locationMP.locationData objectAtIndex:AM_locationMP.indexno]];

where location data is array of location placemark values .

where maplocationVO class is for placemark MapLocationVO *currentMapLocation;

[AM_delegate.locationData addObject: currentMapLocation];

Upvotes: 1

Views: 1886

Answers (3)

Ben Zeeman
Ben Zeeman

Reputation: 11

Why do people post on here when they clearly don't know the answer, or understand what you can and cannot store in NSUserDefaults?

The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs. A default object 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. If you want to store any other type of object, you should typically archive it to create an instance of NSData. For more details, see Preferences and Settings Programming Guide. -- From the documentation

You need to use NSKeyedArchiver to turn the CLLocation data into NSData. Then use NSKeyedUnarchiver to turn the NSData back into CLLocation data.

The next problem to solve is when you archive CLLocation objects and store them in UserDefaults, the don't unarchive properly. The only solution may be to save them to file instead.

Upvotes: 1

Ishu
Ishu

Reputation: 12787

use this

for setting array

NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];

  [standardUserDefaults setObject:yourArray forKey:@"array"];
  [standardUserDefaults synchronize];

and for getting array

NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];

  [standardUserDefaults setObject:yourArray forKey:@"array"];

NSMutableArray *array=[standardUserDefaults objectForKey:@"array"];

Upvotes: 0

meronix
meronix

Reputation: 6176

this should be enough to write it:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:yourArray forKey:@"clArray"];
[defaults synchronize];

Upvotes: 0

Related Questions