The Man
The Man

Reputation: 1462

Saving NSArray of NSDictionary's to File

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...

Upvotes: 0

Views: 465

Answers (2)

Matthias Bauch
Matthias Bauch

Reputation: 90117

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.

Upvotes: 0

Rui Peres
Rui Peres

Reputation: 25907

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

Upvotes: 2

Related Questions