0

I'm trying to save a huge NSArray to a file and then retrieve it...

NSURL *documentsDirectory = 
[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory 
                            inDomains:NSUserDomainMask] lastObject];
NSURL *fileURL = [documentsDirectory 
                  URLByAppendingPathComponent:@"scorecards.dgs"];


NSMutableArray *savedArrayOfScorecards = [NSMutableArray arrayWithContentsOfURL:fileURL];
[savedArrayOfScorecards addObject:currentScoreCard];

[savedArrayOfScorecards writeToURL:fileURL atomically:YES];

NSMutableArray *mynewArray = [NSMutableArray arrayWithContentsOfURL:fileURL];

NSLog(@"%@, %@",[[mynewArray objectAtIndex:0] objectForKey:@"info"], course);

The NSLog comes back with...

(null), BridgeMill

Which BridgeMill should be returned on both sides of the comma. But array won't save...

The Man
  • 1,462
  • 3
  • 23
  • 45

2 Answers2

2

Check if it's being saved correctly:

if([savedArrayOfScorecards writeToURL:fileURL atomically:YES])
{
    NSLog(@"Was saved");
}
else
{
    NSLog(@"Houston we have a problem...");
}

On the documentation:

- (BOOL)writeToURL:(NSURL *)aURL atomically:(BOOL)atomically

You can also use this to check what is going on:

- (BOOL)writeToURL:(NSURL )aURL options:(NSDataWritingOptions)mask error:(NSError *)errorPtr

Rui Peres
  • 25,741
  • 9
  • 87
  • 137
  • Ahaha. :) Ok what you can try: 1) check if the path is **in theory** correct. 2) Use the second method and then inspect the `NSError` – Rui Peres Jun 19 '12 at 14:58
0

what class is course?

writeToURL only supports types that can be converted into plist format. Those types are NSString, NSData, NSArray, or NSDictionary.

If you have other types in your NSArray you have to archive the array.

Community
  • 1
  • 1
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247