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];
Related
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
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
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];
I want to create a new custom property list in iphone applications.How to create in and how to load in iphone class.
Load:
NSString *plistPath = [bundle pathForResource:kDefaultBookmarksPlistName
ofType:#"plist"];
NSArray *defaultBookmarks = [[NSArray alloc]
initWithContentsOfFile:plistPath];
For saving, see the NSArray docs and look up writeToFile
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];