Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-09-2011, 08:06 PM   #1 (permalink)
Registered Member
 
Join Date: Sep 2009
Location: Germany
Posts: 36
Rikco is on a distinguished road
Exclamation 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
Rikco is offline   Reply With Quote
Old 09-09-2011, 08:31 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Rikco View Post
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.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-10-2011, 05:43 AM   #3 (permalink)
Registered Member
 
Join Date: Sep 2009
Location: Germany
Posts: 36
Rikco is on a distinguished road
Default

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!
Rikco is offline   Reply With Quote
Old 09-10-2011, 06:44 AM   #4 (permalink)
Registered Member
 
Join Date: Sep 2009
Location: Germany
Posts: 36
Rikco is on a distinguished road
Default

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];
}
Rikco is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» 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
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:01 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0