2012-02-02 37 views
0

我试图在plist文件中存储一些stings文件,保存过程正常,但应根据特定日期保存这些备注,例如我在2月2日输入备注那么我需要在2月5日输入另一张纸条,当我在这些日期间移动时,我的笔记应该显示在这些日期。如果你能帮助我,我将不胜感激。将文本保存到plist文件中并显示具体日期

这里是我的代码:

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

回答

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]; 
+0

实际上,'NSArray'的'writeToFile:atomically:'方法_will_会导致plist。 – omz 2012-02-02 19:17:34

+0

@omz - 谢谢,你说得对,我会更新我的答案。我的意思是说,他的结构(即使它是一个plist)会在该日期和该日期的数据之间有任何关联。请注意,他的“date”变量甚至没有在saveFilePath方法中使用,也没有在该数组内使用任何NSDate。 – Rayfleck 2012-02-02 19:26:03

+0

你会回顾我的问题吗? – 2012-02-02 22:15:15

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现在包含日期时,该文件是第一保存。要获取保存的上一个的日期,请改为使用NSFileModificationDate。

+0

我应该在哪里使用这段代码? – 2012-02-02 21:47:14

+0

在你的viewDidLoad中,你从plist文件加载数组的相同位置(实际上我看到你已经在使用NSFileManager,所以你可以合并我的if(... existsAtPath)子句和你自己if(fileExists )code。 – 2012-02-02 21:50:51

+0

谢谢,但你的代码与我的代码一样,我创建了一个自定义的日历应用程序,用户可以在其中写入他们的笔记,例如在2月3日用户写东西,这个文本应该保存在2月3日,移动到其他日子,他/她可以写下其他日期,并计算当天的日期 – 2012-02-02 21:58:08

相关问题