You need to retain destinationPath when you create it so it doesn't go
away. You properly retain dataDict, now do the same with
destinationPath and you should be ok. Also you don't need to retain
newRecord; the dictionary will retain it when you add it.
On May 11, 2009, at 3:10 AM, Ben Spam wrote:
Please help me, I'm in serious trouble if I do not sort this out!
I am trying to save a dictionary to a local file, but inserting a
new record crashes my app and I can not fathom why, here is my
code...please let me know if you see anything im doing wrong..
PS: the random key is purely for testing.
#import "MyTestClass.h"
@implementation MyTestClass
- (id) init
{
self = [super init];
//get path
NSArray * paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
destinationPath = [[paths objectAtIndex:0]
stringByAppendingPathComponent:@"filePath.plist"];
//
dataDict = [NSMutableDictionary
dictionaryWithContentsOfFile:destinationPath];
if(!dataDict){ NSLog(@"No File"); dataDict =
[[NSMutableDictionary alloc] init];} else{[dataDict retain];}
return self;
}
- (void) insertNewRecord:(NSMutableDictionary*)newRecord{
[newRecord retain];
[dataDict setValue: newRecord forKey: [NSString
stringWithFormat:@"test%d", (arc4random() % 1000)] ];
bool didSave = [dataDict writeToFile:destinationPath
atomically:YES];
}
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]