0

I am trying to save the current date into my plist then commit the plist to the phone for later use (and update).

The code below is what I am using to try and save the date, however it dose not seem to be working as it should only ever enter "once" (once the file is created the first time) then skip this each time after.. as its been created and available....

However this is not working and I am not sure why, this is the code I have written;

NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *plistFilePath = [documentsDirectory stringByAppendingPathComponent:@"DB-Date.plist"];

    if(![[NSFileManager defaultManager] fileExistsAtPath:plistFilePath])
    {//dosnt exits

        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"DB-Date" ofType:@"plist"];
        NSMutableArray *data = [NSMutableArray arrayWithContentsOfFile:plistPath];
        NSArray *arrValue = [data objectAtIndex:0];

        NSDate *dateValue = [arrValue objectAtIndex:0];
        dateValue = [NSDate date]; //change your value here

        [[data objectAtIndex:0] replaceObjectAtIndex:0 withObject:dateValue]; //value replaced
        [data writeToFile:plistFilePath atomically:YES];

    }

// from here I continue to use the newly created plist file

any help would be greatly appreciated.

HurkNburkS
  • 5,332
  • 19
  • 93
  • 176

2 Answers2

1

So, mainBundle and NSDocumentaryDirectory are two different places...This should be enough to solve your problem ;)

You seem to have a lot of excess code...Let's do this instead in the brackets:

if(![[NSFileManager defaultManager] fileExistsAtPath:plistFilePath])
    {

     NSMutableArray *data = [[NSMutableArray alloc]init];

     NSDate *dateValue  = [NSDate date]; 

    [data addObject:dateValue];

    [data writeToFile:plistFilePath atomically:YES];

   }

If you like the answer please mark as correct. Thanks!

minjiera
  • 336
  • 3
  • 15
  • HOWEVER, an even easier way, is really to use save it in NSUserDefault as applefreak said. – minjiera Sep 19 '12 at 22:01
  • Thank you for your response.. I am trying to read from this but I keep getting (null) back... - short piece of code i am using to do this. ** dbvDict = [NSDictionary dictionaryWithContentsOfFile: plistFilePath]; // read plist data NSDate *oldDate = [dbvDict objectForKey: @"DBVDate"]; NSLog(@"%@", oldDate);** – HurkNburkS Sep 19 '12 at 22:28
  • I see nothing wrong with the storing and retrieving code. The only strange thing seems to be that you are logging NSDate directly, which I believe won't work.... Read "AudioVisualCode"'s answer on this link [link]http://stackoverflow.com/questions/936969/nsdate-and-nsdateformatter-short-format-date-and-time-in-iphone-sdk – minjiera Sep 23 '12 at 23:09
0

You are doing correct way but I am not getting what is going wrong? Can you give example please? BTW if you have only date then why don't you use NSUserDefaults than creating plist file?

AlienMonkeyCoder
  • 7,314
  • 11
  • 69
  • 138
  • regarding the first question, I dont think the file is being created, because every time I get to this if statement it is entered and all the instructions are executed, even though my if statement is saying only check if file is not available. (so was wondering how i could fix/determine this). The second question well I decided to do it this way because i might add other attributes to the plist in the future... and kind of figured whats the harm in one small plist... :P little did I know it would cause this much trouble. – HurkNburkS Sep 19 '12 at 21:47