Search code examples
objective-ciostimestampnsfilemanager

Save NSMutableArray to file with a TimeStamp?


i am trying to save a mutable array into a file with the help of following code

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *ArrayFileName = [documentsDirectory stringByAppendingPathComponent:@"ex.dat"];

[Array writeToFile:ArrayFileName atomically:YES];

but i want to save the array with a timestamp so that i can retrieve it later accordingly. I am not sure how to create or apply a timestamp to the array and how to retrieve it. any help would be appreciated??


Solution

  • Make a NSDictionary with the Array and timestamp as members and @"array" and @"timestamp" as keys. Write the NSDictionary to the file similar to the way you have done it for NSArray.

    Similarly, after retrieving the NSDictionary from the file, you can get the NSArray and timestamp

    using

    NSArray *array =  [dict valueForKey@"array"];
    NSString *timestamp =  [dict valueForKey@"timestamp"];