Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving an array to NSUserDefaults

Tags:

iphone

nsurl

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?

like image 951
some_id Avatar asked Dec 29 '22 00:12

some_id


1 Answers

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.

like image 188
Anomie Avatar answered Jan 15 '23 01:01

Anomie