-2

私はplistを持っており、ユーザーがメモを書くと、そのplistにIDとともに保存し、ユーザーが開くたびに、このユーザー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];

}

保存したメモに 1 つのメモを挿入する

- (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 に答える