0

I have an NSMutableArray and I put URLs in this array.

there is no problem but when I save it with NSUserDefaults it doesn't keeps the content when I load it after closing the app.

I also tried to convert the URL to a string but it also doesn't save it but other NSMutableArrays I have work with strings

I am using Xcode.

can somebody help me?

killerwakka
  • 31
  • 2
  • 9
  • You can take a look here http://stackoverflow.com/questions/2315948/how-to-store-custom-objects-in-nsuserdefaults – Mr. Frank May 13 '13 at 08:26

1 Answers1

4

The reason you are not able to store An Array of URLs in NSUserDefaults is, it allows only primitive Data object Storing, while NSURL is not premitive DataType.

//Define Your URL
NSURL *myURL = [NSURL URLWithString:@"www.google.com"];
NSMutableArray *urlArr = [[NSMutableArray alloc] initWithCapacity:0];
//Add it to Array
for(int i=0;i<5;i++)
    [urlArr addObject:[myURL absoluteString]]; // Convert NSURL to NSString

[[NSUserDefaults standardUserDefaults] setObject:urlArr forKey:@"myURLArr"];
[[NSUserDefaults standardUserDefaults] synchronize];

NSMutableArray *array = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"myURLArr"]];
for(NSString *urlString in array)
    NSLog(@"%@",[NSURL URLWithString:urlString]); // Crete NSURL from NSString
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57