Reputation: 1031
Everything is working fine until I call the saveFile
method (shown below) to write the file back to disk, where it crashes. What am I doing wrong?
This is part of my viewDidLoad
method where I open the file, which works fine.
//Get The Path
[self initPath];
dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:accountsFilePath];
if (accountsArray == nil) {
accountsArray = [[[NSMutableArray alloc]init] autorelease];
}
if (countArray == nil) {
countArray = [[[NSMutableArray alloc]init] autorelease];
}
countArray = [dictionary objectForKey:@"count"];
accountsArray = [dictionary objectForKey:@"username"];
Then I load it into a tableview. I then add some new items to it, which works fine. Then I call this method to save it and it crashes:
-(void)saveFile {
[dictionary setObject:accountsArray forKey:@"username"];
[dictionary setObject:countArray forKey:@"count"];
[dictionary writeToFile:accountsFilePath atomically:YES];
}
Upvotes: 0
Views: 1479
Reputation: 196
// what if the path is not found, file is not load and in turn dictionary is nil
dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:accountsFilePath];
// first time you create accountsArray, countArray
if (accountsArray == nil) {
accountsArray = [[[NSMutableArray alloc]init] autorelease]; }
if (countArray == nil) {
countArray = [[[NSMutableArray alloc]init] autorelease]; }
// hey why you assign accountsArray to a new one again, what about the old one you just init?
// if dictionary is nil, countarray will be nil too
countArray = [dictionary objectForKey:@"count"];
accountsArray = [dictionary objectForKey:@"username"];
//// //// ////////////////////////////////////////////////////////////////
one thing you may want to know
writeToFile will not create a new folder for you
so all folder in accountsFilePath is a must to be exist.
otherwise you may want to create that folder using nsfilemanager
Upvotes: 0
Reputation: 3859
You are autoreleasing countArray
and accountsArray
just after initializing them. Thet may well be already released when you try to save them. Try commenting the autorelease
for both of them (and remember to release them somewhere, maybe in the dealloc
method).
Upvotes: 1
Reputation: 811
You probably already did this, but I would inspect dictionary and accountsFilePath in gdb or by using NSLog right before the writeToFile:atomically: call.
You also might want to share more surrounding code to show what else is going on with respect to this dictionary.
I've been using NSZombie with much success for debugging random crashes as well.
Upvotes: 0
Reputation: 677
Are all your variables in scope? I assume accountsFilePath and dictionary are class variables? If not they might die at the end of viewDidLoad.
The other thing that might bite you is the capacity of your dictionary is too small, or that the iPhone doesn't like you using the setObject method to overwrite key/value pairs like that. Perhaps try calling removeObjectForKey: and then add it back as you have above?
Upvotes: 0