How to update value form plist using Objective-C

I have this challenge when I was doing a ios mobile application. My issue was to update an value of the plist from 0 to 1. šŸ™‚ Sometimes it hard to find in the web with this kind of issue, very simple problem butĀ lots of people can’t find in the web and for those people whoĀ are like me just starting to build more mobile application.

Here is the sample code.

Here is the sample plist what am talking about.

Screen Shot 2016-03-16 at 11.00.33 PM

Here is the code. I am assume that you already know how to get your record from the plist. How this works is when I press the button inside the UITableView it calls out the IProcess. The event capture the button possition of the button and get the indexPath selected.

- (IBAction)IProcess:(UIButton *)sender
{
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:dTableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
    
    // I will be getting the data from my mutable array data
    // with the selectedIndedPath from my uiTableView
    
    NSArray *copySelectedSection = [data objectAtIndex:indexPath.section];

    // Initialize my Value Fom the Plist
    NSMutableDictionary *ValueFromThePlist = [[NSMutableDictionary alloc] init];

    [ValueFromThePlist addEntriesFromDictionary:(NSDictionary *)copySelectedSection];
    
    // Get the value form the data
    NSNumber *isStatus = (NSNumber *)[ValueFromThePlist objectForKey:@"status"];
    
    // Create if statement to verify if the value of the status is 1 then if true set to 0
    if ([isStatus isEqualToNumber:[NSNumber numberWithInt:1]])
    {
        [ValueFromThePlist setObject:[NSNumber numberWithInt:0] forKey:@"status"];
    }
    
    // now since you have updated the value from the record you need to loop again the data from the plist and ammed the value you have selected.
    //

    int ctr = 0;
    NSNumber *indexNumberRow =  [NSNumber numberWithInt:(long)indexPath.row];
    NSMutableArray *newSetOfarray = [NSMutableArray arrayWithCapacity:0];
    for (NSDictionary *mainSection in copySelectedSection) {
        
        if ([indexNumberRow isEqualToNumber:[NSNumber numberWithInt:ctr]])
            
        {
            [newSetOfarray addObject:(NSArray*)ValueFromThePlist];
        }
        else{
            [newSetOfarray addObject:(NSArray*)mainSection];
        }
        
        ctr++;
        
        //
    }
    
    NSString *filePath = [self getDatabasePath];
    
    NSError *error;
    
    NSData *plistData = [NSPropertyListSerialization dataWithPropertyList:(NSDictionary *)newSetOfarray format:NSPropertyListXMLFormat_v1_0 options:NSPropertyListImmutable error:&error];
    
    if(plistData)
    {
        [plistData writeToFile:filePath atomically:YES];
    }
    // DONE DONE DONE 
}

I hope this will help you as well to fix or to solveĀ some simple things just like what I encountered. Happy Coding!!!

Leave a Reply

Your email address will not be published. Required fields are marked *