09-09-2011, 08:06 PM
#1 (permalink )
Registered Member
Join Date: Sep 2009
Location: Germany
Posts: 36
cant write NSMutableDictionary / plist to filepath, I cant find the error
Hello guys,
I created a dictionary data structure by code for a highscore-statistic
Here the full code:
Method for file-path:
Code:
-(NSString *)pathForFile:(NSString *)plistFileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0]stringByAppendingFormat:@"/%@.plist",plistFileName];
NSLog(@"FilePath: %@", filePath);
return filePath;
}
NSLog shows: Applications.......Documents/highscoreWinnerList.plist,
without writing a / into the filename, it shows me ..DocumentshighscoreWinnerList.plist
Code:
-(void)addToHighscoreWinnerList:(int)highscore Name:(NSString *)name Email:(NSString *)email Teilnahme:(BOOL)checkboxState
{
NSString *checkboxString = [NSString stringWithFormat:@"%@", (checkboxState ? @"YES" : @"NO")];
NSString *highscoreString = [NSString stringWithFormat:@"%i", highscore];
NSString *date = [self todayString];
NSString *time = [self currentTimeString];
NSLog(@"PlayerValues: %@, %@, %@, %@, %@, %@", name, email, checkboxString, highscoreString, date, time);
NSArray *playerValues = [NSArray arrayWithObjects:name, email, checkboxString, highscoreString, date, time, nil];
NSArray *playerKeys = [NSArray arrayWithObjects:@"Name", @"Email", @"Teilnahme", @"Highscore", @"Datum", @"Uhrzeit", nil];
NSMutableDictionary *playerData = [NSMutableDictionary dictionaryWithObjectsAndKeys:playerValues, playerKeys, nil];
//NSLog(@"PlayerDataDict: %@", [playerData allValues]);
NSMutableDictionary *timeDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:playerData, time, nil];
//NSLog(@"TimeDict: %@", [timeDict allValues]);
NSMutableDictionary *dateDict = [NSMutableDictionary dictionaryWithObject:timeDict forKey:date];
//NSLog(@"DateDict: %@", [dateDict allValues]);
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:[self pathForFile:@"highscoreWinnerList"]]) {
}
else {
[dateDict writeToFile:[self pathForFile:@"highscoreWinnerList"] atomically:YES];
NSMutableDictionary *checkDict = [NSMutableDictionary dictionaryWithContentsOfFile:[self pathForFile:@"highscoreWinnerList"]];
NSLog(@"%@", [checkDict allValues]);
}
}
NSLog shows me the full structre with all data, but is not writing the file!
And I dont know why.
I´m calling that method with
Code:
[[DataManager sharedManager]addToHighscoreWinnerList:1234 Name:@"Rikco" Email:@"ich@wo.de" Teilnahme:YES];
and it fills the dictionary.
I hope anybody can help me.
Thanks
09-09-2011, 08:31 PM
#2 (permalink )
Cocoa Junkie
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Quote:
Originally Posted by
Rikco
Hello guys,
I created a dictionary data structure by code for a highscore-statistic
Here the full code:
Method for file-path:
Code:
-(NSString *)pathForFile:(NSString *)plistFileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0]stringByAppendingFormat:@"/%@.plist",plistFileName];
NSLog(@"FilePath: %@", filePath);
return filePath;
}
NSLog shows: Applications.......Documents/highscoreWinnerList.plist,
without writing a / into the filename, it shows me ..DocumentshighscoreWinnerList.plist
Code:
-(void)addToHighscoreWinnerList:(int)highscore Name:(NSString *)name Email:(NSString *)email Teilnahme:(BOOL)checkboxState
{
NSString *checkboxString = [NSString stringWithFormat:@"%@", (checkboxState ? @"YES" : @"NO")];
NSString *highscoreString = [NSString stringWithFormat:@"%i", highscore];
NSString *date = [self todayString];
NSString *time = [self currentTimeString];
NSLog(@"PlayerValues: %@, %@, %@, %@, %@, %@", name, email, checkboxString, highscoreString, date, time);
NSArray *playerValues = [NSArray arrayWithObjects:name, email, checkboxString, highscoreString, date, time, nil];
NSArray *playerKeys = [NSArray arrayWithObjects:@"Name", @"Email", @"Teilnahme", @"Highscore", @"Datum", @"Uhrzeit", nil];
NSMutableDictionary *playerData = [NSMutableDictionary dictionaryWithObjectsAndKeys:playerValues, playerKeys, nil];
//NSLog(@"PlayerDataDict: %@", [playerData allValues]);
NSMutableDictionary *timeDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:playerData, time, nil];
//NSLog(@"TimeDict: %@", [timeDict allValues]);
NSMutableDictionary *dateDict = [NSMutableDictionary dictionaryWithObject:timeDict forKey:date];
//NSLog(@"DateDict: %@", [dateDict allValues]);
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:[self pathForFile:@"highscoreWinnerList"]]) {
}
else {
[dateDict writeToFile:[self pathForFile:@"highscoreWinnerList"] atomically:YES];
NSMutableDictionary *checkDict = [NSMutableDictionary dictionaryWithContentsOfFile:[self pathForFile:@"highscoreWinnerList"]];
NSLog(@"%@", [checkDict allValues]);
}
}
NSLog shows me the full structre with all data, but is not writing the file!
And I dont know why.
I´m calling that method with
Code:
[[DataManager sharedManager]addToHighscoreWinnerList:1234 Name:@"Rikco" Email:@"ich@wo.de" Teilnahme:YES];
and it fills the dictionary.
I hope anybody can help me.
Thanks
I don't see any problems with the code above at a glance.
It's generally easier and safer to use methods like stringByAppendingPathComponent to build path strings than to try to construct the path yourself.
My guess is that there's something wrong with the path you are constructing, and the write is failing. I would suggest single-stepping through the code in the debugger.
Rewrite your call to writeToFile:atomically to save the result of the method call to a boolean, and examine it.
I would also save the pathname you get from your pathForFile method to a local variable, and look at that in the debugger. Take a look at it and see if it has any problems.
You might also take the path from pathForFile and try to use it to create an NSURL object using the fileURLWithPath NSURL class method. That will fail if the path is not well-formed.
09-10-2011, 05:43 AM
#3 (permalink )
Registered Member
Join Date: Sep 2009
Location: Germany
Posts: 36
Thank your for your response!
I tried what you said and that brought me one step forward:
the path is full correct and was it every time,
and I have found the problem source:
I tried to write just the arrays from above, and that works fine
but when i´m writing the first dictionary, than there are only the data from the first array, so there is an error in creating the dictionary
thank you!
09-10-2011, 06:44 AM
#4 (permalink )
Registered Member
Join Date: Sep 2009
Location: Germany
Posts: 36
Problem solved:
I had to alloc and init the dicionarys, and create a rootDict which represent the whole file, now I get my .plist
Date
--Time
----Name
----Email
----.......
Code:
-(void)addToHighscoreWinnerList:(int)highscore Name:(NSString *)name Email:(NSString *)email Teilnahme:(BOOL)checkboxState
{
NSString *checkboxString = [NSString stringWithFormat:@"%@", (checkboxState ? @"YES" : @"NO")];
NSString *highscoreString = [NSString stringWithFormat:@"%i", highscore];
NSString *date = [self todayString];
NSString *time = [self currentTimeString];
NSLog(@"PlayerValues: %@, %@, %@, %@, %@, %@", name, email, checkboxString, highscoreString, date, time);
NSArray *playerValues = [NSArray arrayWithObjects:name, email, checkboxString, highscoreString, date, time, nil];
NSArray *playerKeys = [NSArray arrayWithObjects:@"Name", @"Email", @"Teilnahme", @"Highscore", @"Datum", @"Uhrzeit", nil];
NSMutableDictionary *timeDict = [[NSMutableDictionary alloc]initWithObjects:playerValues forKeys:playerKeys];
//NSLog(@"TimeDict: %@", [playerData allValues]);
NSMutableDictionary *dateDict = [[NSMutableDictionary alloc]initWithObjectsAndKeys:timeDict, time, nil];
//NSLog(@"DateDict: %@", [timeDict allValues]);
NSMutableDictionary *rootDict = [[NSMutableDictionary alloc]initWithObjectsAndKeys:dateDict, date, nil];
//NSLog(@"RootDict: %@", [dateDict allValues]);
NSString *filePath = [self pathForFile:@"TestData"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:filePath]) {
}
else {
[rootDict writeToFile:filePath atomically:YES];
}
NSArray *checkFile = [NSArray arrayWithContentsOfFile:filePath];
NSLog(@"CheckArray: %@", checkFile);
[timeDict release];
[dateDict release];
[rootDict release];
}
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 413
10 members and 403 guests
apatsufas , Eclectic , fiftysixty , JackReidy , jeroenkeij , teebee74 , tim0504 , UMAD , yomo710 , yuncarl28
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,672
Threads: 94,121
Posts: 402,904
Top Poster: BrianSlick (7,990)
Welcome to our newest member, yuncarl28