Add & Remove Array Items inside a plist - iphone

I have problem with adding and removing items in an array inside a plist file in XCODE.
I'm able to read the array by following code:
// Path to the plist (in the application bundle) ------>>>>>
NSString *path = [[NSBundle mainBundle] pathForResource:
#"Fav" ofType:#"plist"];
// Build the array from the plist ------>>>
NSDictionary *favs = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
Resepies = [favs objectForKey:#"Root"];
And this my plist structure
The senario is to let the user add and remove specific item from the array at a time.

try this code -
NSString *path = [[NSBundle mainBundle] pathForResource: #"Fav" ofType:#"plist"];
// Build the array from the plist ------>>>
NSDictionary *favs = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
Resepies = [favs objectForKey:#"Root"];
[Resepies addObject:addYourObjectHere];
//then add this array into dictonary ;
[favs setObject:Resepies forKey:#"Root"];
// now write dictionary to plist
[favs writeToFile:yourPlistName atomically:YES];

You can't modify files in the application bundle, which is what your code above is attempting to do.
If the file is supposed to be modifiable, you need to move it into the documents folder first (say, on first run) and then read / write to that one subsequently. There are plenty of questions dealing with how to do this, for example : create plist and copying plist to document directory

Related

Access .plist Values

I am building an app that has a pickerview, thus, I had created a .plist file to make the keys and values. The only issue is I want to be able to access the .plist values. The thing is, the values are name strings which when selected on the picker view i would like to create an if statement where i call a mobile number. I need guide on access the values if that is possible
Any idea on how to carry this out?
NSString *pathToFile = [[NSBundle mainBundle] pathForResource:#"myList" ofType:#"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:pathToFile];
NSString *path = [[NSBundle mainBundle] pathForResource: // here data is file nale of plist
#"data" ofType:#"plist"];
// Build the dictionary from the plist
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:path];

How to add a key-value pair to a plist file in Objective-C

I'm working on an iPhone app and I need to add a new key-value pair to an existing plist file using objective-c. This is what I've tried so far:
NSString *myFile=[[NSBundle mainBundle] pathForResource:#"Favourites" ofType:#"plist"];
dict = [[NSMutableDictionary alloc] initWithContentsOfFile:myFile];
[dict setObject:textContent forKey:keyName];
[dict writeToFile:myFile atomically:YES];
However, when I do this it doesn't write it to the file. I've only seen resources based on either changing the value of a key or adding to another key. Is there another way to accomplish this?
Thank you for your time
You cannot make any changes in the bundle. So what you have to do is copy the .plist file into your documents directory and do the manipulation there.
Something along these lines:
//check for plist
//Get documents directory's location
NSArray*docDir=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString*filePath=[docDir objectAtIndex:0];
NSString*plistPath=[filePath stringByAppendingPathComponent:#"Favourites.plist"];
//Check plist's existance using FileManager
NSError*err=nil;
NSFileManager*fManager=[NSFileManager defaultManager];
if(![fManager fileExistsAtPath:plistPath])
{
//file doesn't exist, copy file from bundle to documents directory
NSString*bundlePath=[[NSBundle mainBundle] pathForResource:#"Favourites" ofType:#"plist"];
[fManager copyItemAtPath:bundlePath toPath:plistPath error:&err];
}
//Get the dictionary from the plist's path
NSMutableDictionary*plistDict=[[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
//Manipulate the dictionary
[plistDict setObject:textContent forKey:keyName];
//Again save in doc directory.
[plistDict writeToFile:myFile atomically:YES];

how to add and retrieve data from plist programmatically

hello all
I am new to objective c and i want to insert data dynamically/programmatically into plist.please help me.Here is my plist structure
root
|_Client1
|_report1
|_application1
|_application2
|_report2
|_application3
|_Client2
|_report1
Now i want to add and retrieve data dynamically to application1,application2 in my plist
please help me
Reading content of .plist file ..
NSString* plistPath = [[NSBundle mainBundle] pathForResource:#"league" ofType:#"plist"];
contentArray = [NSArray arrayWithContentsOfFile:plistPath];
Writing to .plist file.
NSMutableDictionary * myDictionary;
NSString* plistPath = [[NSBundle mainBundle] pathForResource:#"league" ofType:#"plist"];
[myDictionary writeToFile:plistPath atomically:YES];
Note that:
Dictionary must contain plist objects (instances of NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary). And dictionary keys must be NSString objects

can't read plist from resources

in my resources directory i have a file called lsf.plist
i want to load this file (dictionary) but i always get null as content of the file. i am using the following code. i've verified that the file is in the app after the build.
self.path = [[NSBundle mainBundle] pathForResource:#"lsf" ofType:#"plist"];
NSLog(self.path);
self.lsf = [NSMutableArray arrayWithContentsOfFile:path];
NSLog(#"%#",lsf);
The first log-output shows the path and the second one gives me null....
it would be great if you can help me to solve this issue!
Br,
martin
Is root of your plist array or dictionary?
Also you dont need to use self.
Heres an example from a project of mine:
NSString* path = [[NSBundle mainBundle] pathForResource:#"property" ofType:#"plist"];
NSDictionary *newDict = [NSDictionary dictionaryWithContentsOfFile:path];

How to populate UIPickerView with .plist file?

I want to populate one.plist files from a number of .plist files as per the user selection .
How to populate UIPickerView with a .plist file ?
I don't understand your question well but here is what I guess. You may want to load data from a .plist file and then fill in the UIPickerView.
Here is a sample code for loading a .plist file:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:#"money.plist"];
NSDictionary *plistDictionary = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];