Search code examples
iphonensurl

Saving an array to NSUserDefaults


How is an array saved to NSUserDefaults?

I have the following code which tries to store an array of NSURLs

NSArray *temp = [[NSArray alloc] initWithArray:[mySingleton sharedMySingleton].sharedURLS];
NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
[defs setObject:temp forKey:@"URLs"];

but I get a warning

-[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value

What is the correct way to store this or collection of NSURLS?


Solution

  • You can't directly store an NSURL in NSUserDefaults, only NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary; also, any NSArray or NSDictionary may only contain objects of these types. You'll have to convert the NSURLs into one of these types, most likely by using absoluteString to convert them into NSStrings.