简体   繁体   中英

Write data to Plist

I have plist which contains a dictionary say dict and in dict I have 3 arrays for keys A,B,C. Now I want to add an item in A,B and C under particular situation. For this my doing like this...

    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *documentsDirectory = [path objectAtIndex:0];
    NSString *plistPath = [documentsDirectory stringByAppendingString:@"MovingChecklist.plist"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    if (![fileManager fileExistsAtPath: plistPath]) 
    {
        NSString *bundle = [[NSBundle mainBundle] pathForResource:@"MovingChecklist" ofType:@"plist"];
        [fileManager copyItemAtPath:bundle toPath: plistPath error:&error];
    }
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    NSArray *array1 = [dict objectForKey:@"A"];
    NSArray *array2 = [dict objectForKey:@"B"];
    NSArray *array3 = [dict objectForKey:@"C"];

    NSMutableArray *myArray;
    if (currentMovingList == KMovingListTypeA) {
        myArray = [NSMutableArray arrayWithArray:array1];
        [myArray addObject:nameTextView.text];
        [dict setValue:myArray forKey:@"A"];

    }
    if (currentMovingList == KMovingListTypeB) {
        myArray = [NSMutableArray arrayWithArray:array2];
        [myArray addObject:nameTextView.text];
        [dict setValue:myArray forKey:@"B"];
    }
    if (currentMovingList == KMovingListTypeC) {
        myArray = [NSMutableArray arrayWithArray:array3];
        [myArray addObject:nameTextView.text];
        [dict setValue:myArray forKey:@"C"];
    }
[dict writeToFile:plistPath atomically: YES];

But the plist is not changing. Suggest me what mistake m doing.

我认为您应该将此设置为可变

NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM