从Plist删除不起作用?

时间:2013-05-13 04:46:42

标签: ios plist

我有一个plist,当用户写下笔记时,我将其保存到该plist及其id中,每次用户打开时,它将检查该用户id是否在plist中有任何注释并在uitableview中显示。也是用户可以删除笔记,但当我尝试执行以下过程时,我得到了例外

1.在视图中加载检查用户是否有任何先前的注释 2.使用用户ID检查plist 3.如果匹配检索相应的注释 4.并将其保存到一个可变数组中。因此,当用户首先添加一个新注释时,我们使用前一个可变数组存储新注释并再次将其写入plist //不能为我工作。 5.当用户删除笔记然后将其更新到plist

1 个答案:

答案 0 :(得分: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];
}