I’m very new to memory management, and I have a question regarding saving a date pickers date. This is the code that I use to save inputted text:
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
event1Field.text = [array objectAtIndex:0];
event2Field.text = [array objectAtIndex:1];
}
How would I edit this to save a date pickers date rather than inputted text? And how would I edit the viewDidLoad method as well? I usually just input the data of the picker in there, like this:
- (void)viewDidLoad {
NSDate *now = [NSDate date];
[datePicker setDate:now animated:YES];
}
But I’m not sure how I would go about loading it into it’s saved state. Sorry if these are dumb questions, I’m still pretty new and learning as I go.
Thank you!
The simplest way to persist data is to use
NSUserDefaultswhich is provided by the Foundation framework. It’s basically just a key value store which allows you to save small amounts of data.First and foremost, saving data from a date picker looks something akin to this:
Now when we want to pull this data back out and populate our date picker, we could do something like the following…