Search code examples
iosobjective-cplisttableviewcheckmark

Replaceing a string on a plist


I am making some confing settings using a tableView using a plist File to fill the data I am working around this code about 2 weeks and this appears to work.

This is my code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

    if ([[[week objectAtIndex:indexPath.row] objectForKey:@"checkmark"]  isEqual:@"0"]) {
        [selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
        [[week objectAtIndex:indexPath.row] setObject:@"1" forKey:@"checkmark"];
    }
    else {
        [selectedCell setAccessoryType:UITableViewCellAccessoryNone];
        [[week objectAtIndex:indexPath.row] setObject:@"0" forKey:@"checkmark"];
    }

    [[week objectAtIndex:indexPath.row] writeToFile:@"week.plist" atomically:YES];

    NSLog(@"%@, %@", selectedCell.textLabel.text,[[week objectAtIndex:indexPath.row] objectForKey:@"checkmark"]);
    NSLog(@"Array de week seleccionando %@", week);
}

Everything seems to work fine on the debbug area (that's why I use the nslogs), and my problem is That can't save the changes on the plist file, it seem it saving in some kind of cache or something like that.

Update

I've also tried with this code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *plistPath = [rootPath stringByAppendingPathComponent:@"week.plist"];
    NSDictionary *plistDict = [NSDictionary dictionaryWithObjects:week forKeys:week];

    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
                                                                   format:NSPropertyListXMLFormat_v1_0
                                                         errorDescription:nil];

    if ([[[week objectAtIndex:indexPath.row] objectForKey:@"checkmark"]  isEqual:@"0"]) {
        [selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
        NSString *uno = @"1";
        [[week objectAtIndex:indexPath.row] setObject:uno forKey:@"checkmark"];
    }
    else if ([[[week objectAtIndex:indexPath.row] objectForKey:@"checkmark"]  isEqual:@"1"]) {
        [selectedCell setAccessoryType:UITableViewCellAccessoryNone];
        NSString *cero = @"0";
        [plistData writeToFile:plistPath atomically:YES];
    }
    [[week objectAtIndex:indexPath.row] setObject:cero forKey:@"checkmarck"];
    NSLog(@"%@, %@", selectedCell.textLabel.text,[[week objectAtIndex:indexPath.row] objectForKey:@"checkmark"]);
    NSLog(@"Array de checkmarks %@", week);
}

And I have had the same result on the Debug Area:

2014-07-07 22:11:21.384 experiment table view[2264:60b] Thursday, 0
2014-07-07 22:11:21.385 experiment table view[2264:60b] Array de week seleccionando (
    {
    Name = Sunday;
    checkmarck = 0;
},
    {
    Name = Monday;
    checkmarck = 1;
},
    {
    Name = Tuesday;
    checkmarck = 0;
},
    {
    Name = Wednesday;
    checkmarck = 0;
},
    {
    Name = Thursday;
    checkmarck = 0;
},
    {
    Name = Friday;
    checkmarck = 1;
},
    {
    Name = Saturday;
    checkmarck = 0;
}
)

Which is the same result that I get


Solution

  • If the Plist is in the bundle, you cannot write into it. 'In the bundle' means that you create the Plist by adding a new file in XCODE manually.

    You can only write into a Plist that you create programmatically. Such a Plist is not in the bundle, but in your app's document directory. The path of the Plist is

    NSArray *paths = NSSearchPathForDirectoriesInDomains
    (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fileName = @"week.plist";
    NSString *path = [NSString stringWithFormat:@"%@/%@", documentsDirectory, fileName];