将文本保存在具有特定日期的 plist 文件中

发布于 2025-01-01 15:29:41 字数 1554 浏览 3 评论 0原文

我正在尝试将一些内容存储到 plist 文件中,保存过程工作正常,但这些注释应该根据特定日期保存,例如我在 2 月 2 日输入注释,然后我需要在 2 月 5 日输入另一个注释,并且当我在这些日期搬家时,我的笔记应该显示在这些日期。如果您帮助我,我将不胜感激。

这是我的代码:

//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];
}

I am trying to store some stings into a plist file, the saving process works fine, but these notes should be saved based on a specific date, for example I enter a note on 2 Feb then I need enter another note on 5 Feb, and when I move during these dates my notes should show on these dates. I would be grateful if you help me.

Here is my code:

//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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事灯 2025-01-08 15:29:41

看来您正在将数组保存到文件中,但使用“.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];

It appears you're saving an array to a file, but using a ".plist" file extension. While the file created will be a plist file, there is no date information in the content.

Instead of an array, you could use an NSDictionary. Use the current date as the key, and the array you're using now as the object. Like this:

 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];
够运 2025-01-08 15:29:41

我认为您想要做的是使用 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 现在包含文件首次保存的日期。要获取上次保存的日期,请使用 NSFileModificationDate。

I think what you want to do is use the NSFileManager to find out when the file was saved, like this:

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 now contains the date when the file was first saved. To get the date when it was last saved, use NSFileModificationDate instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文