我正在尝试将一些stings存储到plist文件中,保存过程正常,但这些注释应该根据特定日期保存,例如我在2月2日输入注释然后我需要在5上输入另一个注释2月,当我在这些日期搬家时,我的笔记应该显示在这些日期。如果你帮助我,我将不胜感激。
这是我的代码:
//Save Setting ///////////////////
- (NSString *) saveFilePath
{
NSArray *pathArray =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSDate *date = [NSDate date];
return [[pathArray objectAtIndex:0] stringByAppendingPathComponent:@"note.plist"];
}
- (void)applicationWillTerminate:(UIApplication *)application
{
NSArray *values = [[NSArray alloc] initWithObjects:saveTextToday.text ,nil];
[values writeToFile:[self saveFilePath] atomically:YES];
[values release];
}
- (void)viewDidLoad
{
////Save Setting /////////////////////////////////////////////////
NSString *myPath = [self saveFilePath];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:myPath];
if (fileExists)
{
NSArray *values = [[NSArray alloc] initWithContentsOfFile:myPath];
saveTextToday.text = [values objectAtIndex:0];
[values release];
}
// notification
UIApplication *myApp = [UIApplication sharedApplication];
// add yourself to the dispatch table
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillTerminate:)
name:UIApplicationWillTerminateNotification
object:myApp];
}
答案 0 :(得分:2)
您似乎要将数组保存到文件中,但使用“.plist”文件扩展名。虽然创建的文件将是plist文件,但内容中没有日期信息。
您可以使用NSDictionary而不是数组。使用当前日期作为键,以及您现在使用的数组作为对象。像这样:
NSMutableDictionary *myDictionary = [NSMutableDictionary alloc] initWithContentsOfFile:myPath];
NSString *todayKey = [self showPersianFullDate]; // or [self showPersianFullDate:[NSDate date]]
[myDictionary setObject:saveTextToday.text forKey:todayKey];
[myDictionary writeToFile:[self saveFilePath] atomically:YES];
答案 1 :(得分:1)
我认为你想要做的是使用NSFileManager找出保存文件的时间,如下所示:
NSString *path = @"the path you've saved you plist to";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDate *creationDate = nil;
if ([fileManager fileExistsAtPath:path])
{
NSError *error = nil;
NSDictionary *attributes = [fileManager attributesOfItemAtPath:path error:&error];
creationDate = [attributes objectForKey:NSFileCreationDate];
}
creationDate现在包含首次保存文件的日期。要获取保存 last 的日期,请改用NSFileModificationDate。