getting data from plist to NSMutableArray and writing the NSMutableArray to same plist iPhone
Asked Answered
E

3

0

Using this code I am trying to get the data from Templates.plist file but I am getting null value in array.

//from plist
NSString *path = [[NSBundle mainBundle] pathForResource:@"Templates" ofType:@"plist"];
NSMutableArray *arry=[[NSMutableArray alloc]initWithContentsOfFile:path];

NSLog(@"arrayArray:::: %@", arry);

This is my plist file:

Plist file

Also I want to know how can I add more strings to this plist file and delete a string from this plist.

Encrimson answered 8/2, 2012 at 12:32 Comment(0)
D
3

First off you cannot write to anything in you mainBundle. For you to write to you plist you need to copy it to the documents directory. This is done like so:

- (void)createEditableCopyOfIfNeeded 
{
     // First, test for existence.
     BOOL success;

     NSFileManager *fileManager = [NSFileManager defaultManager];
     NSError *error;
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documentsDirectory = [paths objectAtIndex:0];
     NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:@"Template.plist"];
     success = [fileManager fileExistsAtPath:writablePath];

     if (success) 
         return;

     // The writable file does not exist, so copy from the bundle to the appropriate location.
     NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Template.plist"];
     success = [fileManager copyItemAtPath:defaultPath toPath:writablePath error:&error];
     if (!success) 
         NSAssert1(0, @"Failed to create writable file with message '%@'.", [error localizedDescription]);
}

So calling this function will check if the file exists in the documents directory. If it doesn't it copies the file to the documents directory. If the file exists it just returns. Next you just need to access the file to be able to read and write to it. To access you just need the path to the documents directory and to add your file name as a path component.

NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [docDir stringByAppendingPathComponent:@"Template.plist"];

To get the data from the plist:

NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];

To write the file back to the documents directory.

    [array writeToFile:filePath atomically: YES];
Decca answered 8/2, 2012 at 17:3 Comment(2)
no success still getting the null value for array, I have added your method - (void)createEditableCopyOfIfNeeded and then call it from view DidLoad, as [self createEditableCopyOfIfNeeded], after calling this function I have used your code to read the plist file but getting null valueEncrimson
I had run into that issue once. What you might have to do is reset the simulator. What this code does is if there is a file already there called Template.plist it will not override it. So if the file is already there then this code will not work for you until you remove it. This cane done by running the simulator and clicking iOS simulator menu and pressing reset content and settings. This will wipe the simulator clean. The other options is use NSLog(@"%@", filePath); and just follow the path until you find the file and remove it.Decca
I
2
-(NSMutableArray *)start1

 {

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"Templates.plist"];



if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]){

    plistArr = [NSMutableArray arrayWithContentsOfFile: plistPath];

}
else {
    plistArr = [[NSMutableArray alloc] init];

}
return plistArr;
}


- (void)createNewRecordWithName:(NSMutableDictionary *)dict

 {

plistArr=[self start1];
[plistArr addObject: dict];
//[dict release];
[self writeProductsToFile:plistArr];

 }

  - (void)writeProductsToFile:(NSMutableArray *)array1 {

   NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"Template.plist"];

[array1 writeToFile:plistPath atomically:YES];

   }
Isbella answered 8/2, 2012 at 12:36 Comment(4)
thanx KAREEM MAHAMMED, can you please tell me the link to this tutorial, so that I would come to know that whats going on in this codeEncrimson
Using start method you are getting all values. SImilarly by creatingMethod you are going to add new feilds to YOur plist fileIsbella
Here you have to use NSString instead of NSMutableDictinoryIsbella
have tried your code, but its not returning any values in the array. its emptyEncrimson
D
0

To get a plist into a mutable array, use this class method:

NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:path];

Then, add/delete strings from the array. To save the array back to a plist, use:

[array writeToFile:path atomically:YES];
Donor answered 8/2, 2012 at 15:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.