Saving / recalling data .plist file

Discussion in 'iOS Programming' started by 87vert, Sep 25, 2010.

  1. 87vert macrumors 6502

    87vert

    Joined:
    Oct 7, 2008
    Location:
    Pittsburgh, PA
    #1
    I am building a "mood" tracker application. I am currently trying to get the data saved and recovered. I have it saving the date to a plist called crazydata.plist. But I cannot figure out how to save more than 1 day's worth. Since I am only grabbing the data from index 0. I need to grab the data from however many index's there are until the end of file, add that to an array then add the currently selected mood and date to the file.

    I was not sure what the best method was so I am just currently fumbling around (this is my first iphone app). Just looking for some guidance on where to look to go.

    Should I even be using a plist? Maybe core data. I have another screen that I will read back the plist and extract the dates and mood to recall what the mood was on a particular day.

    Any ideas? Just don't want to spend days going down the wrong path. :D


    Code:
    -(void) saveData: (NSString *) level and: (NSString *) date
    {
    	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    	NSString *documentsDirectory = [paths objectAtIndex:0];
    	NSString *path = [documentsDirectory stringByAppendingPathComponent:@"crazydata.plist"];
    	BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:path];
    	
    	//combine selected and dateString into one string with " | " as seperator
    	NSString *combinedData = [[NSString alloc] init];
    	selected = [selected stringByAppendingString: @" | "];
    	combinedData = [selected stringByAppendingString: dateString];
    	
    	if (fileExists) {		
    		//pulls data from existing file saves to oldvalues array
    		NSArray *oldvalues = [[NSArray alloc] init];
    		oldvalues = [NSArray arrayWithContentsOfFile:path];
    		NSString *oldDataString = [[NSString alloc] init];
    		NSLog(@"%@", oldvalues);
    		//only saving data from Index one, find someway to index++ loop until done
    		oldDataString = [oldvalues objectAtIndex:0];
    		NSArray *values = [[NSArray alloc] initWithObjects:combinedData, oldDataString, nil];
    		[values writeToFile:path atomically:YES];
    		[values autorelease];
    	}
    	else {
    		//create new crazydata.plist if non-existing
    		NSArray *values = [[NSArray alloc] initWithObjects:combinedData, nil];
    		NSLog(@"%@", values);
    		[values writeToFile:path atomically:YES];
    		[values autorelease];
    	}
    Here is what the plist looks like.
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <array>
    	<string>Crazy | 09 25 2010</string>
    	<string>Somewhat Crazy | 09 25 2010</string>
    </array>
    </plist>
    
     
  2. 87vert thread starter macrumors 6502

    87vert

    Joined:
    Oct 7, 2008
    Location:
    Pittsburgh, PA
    #2
    Not enough info? Should I just start learning core data and migrate to that?
     
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    The problem area seems to be more fundamental than that.

    It seems you need to learn how to write loops. You should probably do that by writing simpler programs first, until you're completely familiar with the fundamentals.

    If you're learning from a book, post it's title and author. Also post any other programming books you worked through before that.

    If you're not learning from a book, you probably should be, because you seem to be missing some very fundamental things.
     
  4. 87vert thread starter macrumors 6502

    87vert

    Joined:
    Oct 7, 2008
    Location:
    Pittsburgh, PA
    #4
    I do know how to make loops but I was not sure if a loop would work in that situation.

    I will look into adding a loop to grab all of the index's

    I have the Kochan Obj-C 2.0 and the Hillegass 3rd edition "Cocoa Programming for Mac OS-X"

    Thanks
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    That uncertainty suggests you don't have sufficient understanding of loops, and when to use them.

    I don't know a nicer way of saying this, but I think you may already be on the wrong path. You seem to be trying to write a program that's beyond your current skill level. It's common for beginners to underestimate the amount of difficulty or the skills needed to produce a real-world non-trivial program. You should write (and debug) a lot of simpler programs first. Those programs may seem irrelevant when you have an idea that you want to write a program for, but they give you the foundation for writing all programs, so they are extremely important. Training is everything.
     
  6. 87vert thread starter macrumors 6502

    87vert

    Joined:
    Oct 7, 2008
    Location:
    Pittsburgh, PA
    #6
    I figured it out using an NSDictionary and NSMutableDictionary

    I thought it was going to be a simple app. :D

    Thanks
     

Share This Page