-2

我有一个 plist,当用户写笔记时,我会连同他们的 id 一起保存到该 plist 中,每次用户打开时,它都会检查这个用户 id 是否在 plist 中有任何笔记并将其显示在 uitableview 中。用户也可以删除笔记但是当我尝试执行以下过程时出现异常

1.查看 didload 检查用户是否有任何以前的笔记 2.检查用户 ID 的 plist 3.如果匹配检索相应的笔记 4.并将其保存到可变数组中。所以当用户首先添加新笔记时,我们使用以前的可变数组来存储新笔记并再次将其写入 plist //不适合我。5.当用户删除笔记然后更新它到plist

4

1 回答 1

1

我假设你有一个类似于这个的结构

[
    {
        "UserID": 1,
        "Notes": [
            {
                "NoteID": 1,
                "Desc": "Description"
            },{
                "NoteID": 2,
                "Desc": "Description"
            }
        ]
    }
]

文档目录中的 Plist 文件路径

- (NSString *)userNotesFilePath{

   NSString *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                              NSUserDomainMask,
                                                              YES)[0]; 

    return [documents stringByAppendingPathComponent:@"UserNotes.plist"];

}

方法获取用户 ID 的已保存笔记

- (NSArray *)savedNotesForUserID:(NSInteger)userID{

    NSString *filePath = [self userNotesFilePath];
    NSArray *savedNotes = [NSArray arrayWithContentsOfFile:filePath];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"UserID = %d",userID];

    NSDictionary *user = [[savedNotes filteredArrayUsingPredicate:predicate]lastObject];

    return  user[@"Notes"];
}

将新的笔记数组保存到特定的用户 ID

- (void)insertNotes:(NSArray *)notesArray  forUserID:(NSUInteger)userID{

    if (!notesArray) {
        return;
    }

    NSString *filePath = [self userNotesFilePath];
    NSMutableArray *savedNotes = [NSMutableArray arrayWithContentsOfFile:filePath];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"UserID = %d",userID];

    NSInteger index = [savedNotes indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop){
        return [predicate evaluateWithObject:obj];
    }];

    NSMutableDictionary *user = [savedNotes[index] mutableCopy];
    user[@"Notes"] = notesArray;

    [savedNotes replaceObjectAtIndex:index withObject:user];
    [savedNotes writeToFile:filePath atomically:YES];

}

在已保存的笔记中插入一条笔记

- (void)insertNote:(NSDictionary *)userNote  forUserID:(NSUInteger)userID{

    if (!userNote) {
        return;
    }

    NSString *filePath = [self userNotesFilePath];
    NSMutableArray *savedNotes = [NSMutableArray arrayWithContentsOfFile:filePath];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"UserID = %d",userID];

    NSInteger index = [savedNotes indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop){
        return [predicate evaluateWithObject:obj];
    }];

    NSMutableDictionary *user = [savedNotes[index] mutableCopy];

    NSMutableArray *savedUserNotes = [user[@"Notes"] mutableCopy];
    if (!savedUserNotes) {
        savedUserNotes = [NSMutableArray array];
    }

    [savedUserNotes addObject:userNote];

    user[@"Notes"] = savedUserNotes;

    [savedNotes replaceObjectAtIndex:index withObject:user];
    [savedNotes writeToFile:filePath atomically:YES];
}
于 2013-05-13T08:21:56.700 回答