How to use plist programmatically? [duplicate] - ios

This question already has answers here:
How do I load a plist file from disk as a NSDictionary on iOS?
(3 answers)
Closed 7 years ago.
I am new in this field. So, can anyone explain how to use plist programmatically? From, where can I start , so that I can learn it properly?

You can access plist as same as NSDictionary. Plist provide data in key value formate.
-you can add your own plist file and use your plist as this way.
NSString *path = [[NSBundle mainBundle] pathForResource: #"plistname" ofType:#"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: path];
NSString *temp = [dict objectForKey:[NSString stringWithFormat:#"%#-Your key name",key]];

Related

iOS - extracting data from a plist not working

This is a routine exercise. I have done it a number of times in my current project and it has worked fine. I copied the code line for line, same initializations. My plist data goes into a dictionary but then it does not go into its respective arrays in their initializations. I have a method called initArraysPlist
-(void)initArraysPlist{
NSString *path1 = [[NSBundle mainBundle] pathForResource:#"trainerProfile" ofType:#"plist"];
// Load the file content and read the data into arrays
NSDictionary *dict1 = [[NSDictionary alloc] initWithContentsOfFile:path1];
trainerNames = [dict1 objectForKey:#"Names"];
trainerIcons = [dict1 objectForKey:#"Icons"];
trainerFactSheet= [dict1 objectForKey:#"Fact Sheet"];
trainerFocus = [dict1 objectForKey:#"Focus"];
trainerContactInfo= [dict1 objectForKey:#"Contact Info"];
}
Ive done this a few times and it currently works in my code. all the values are correct. Ive checked it many times. when
Please read the comments for the each line.
NSString *path1 = [[NSBundle mainBundle] pathForResource:#"trainerProfile" ofType:#"plist"]; // **check if your plist is actually added in Bundle.If its there move to second line , if not then add plist in bundle.**
NSDictionary *dict1 = [[NSDictionary alloc] initWithContentsOfFile:path1];// **if plist is added in bundle , then check if you are getting value for dict1 . If no then you might be making some mistake in plist structure.**
For more clarifications please post your plist if possible.
Please try this code it may be helpful to you
// Read plist from bundle and get Root Dictionary out of it
NSDictionary *dictRoot = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"data" ofType:#"plist"]];
// Your dictionary contains an array of dictionary
// Now pull an Array out of it.
NSArray *arrayList = [NSArray arrayWithArray:[dictRoot objectForKey:#"catlist"]];
// Now a loop through Array to fetch single Item from catList which is Dictionary
[arrayList enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
// Fetch Single Item
// Here obj will return a dictionary
NSLog(#"Category name : %#",[obj valueForKey:#"category_name"]);
NSLog(#"Category id : %#",[obj valueForKey:#"cid"]);
}];

Getting XML values [duplicate]

This question already has answers here:
Parsing XML file with NSXMLParser - getting values
(4 answers)
Closed 7 years ago.
I have an xml which i want to parse it , i succesed to do this but i have a question about the structure of this xml .
<BIBLEBOOK bnumber="1" bname="Genesis" bsname="Gen">
<CHAPTER cnumber="1">
<VERS vnumber="1">At the first God made the heaven and the earth.</VERS>
<VERS vnumber="2">And the earth was waste and without form; and it was dark on the face of the deep: and the Spirit of God was moving on the face of the waters.</VERS>
How can i get the value for bname ?
This is how i parse the xml:
NSString *xmlString = [[NSBundle mainBundle] pathForResource:#"basic_english" ofType:#"xml"];
NSData *xmlData = [NSData dataWithContentsOfFile:xmlString];
NSString* dataStr = [[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding];
NSDictionary *xmlDoc = [NSDictionary dictionaryWithXMLString:dataStr];
Try this. Hope it will help you.
NSString *bname = [xmlDoc valueForKey:#"_bname"];

Unable to read plist from a bundle outside my own app

What am I doing wrong? Trying to read Info.plist of Calendar.app into an array but I get nil array. Here is my code:
NSBundle* xyzbundle = [NSBundle bundleWithPath:#"/Applications/Calendar.app/Contents"];
// Path to the plist
NSString *path = [xyzbundle pathForResource:
#"Info" ofType:#"plist"];
// Build the array from the plist
NSMutableArray *array2 = [[NSMutableArray alloc] initWithContentsOfFile:path];
// Show the string values
for (NSString *str in array2)
NSLog(#"--%#", str);
Thank you for your help.
You cant do it. iOS kernel creates a sandbox environment for each of the application. Any application can not read/write data of other application. There are some possible exceptions but in general you cant do it.

Creating dictionary from plist and am getting back null

I have a plist file in my project that looks like this :
and im doing this
NSURL *file = [[NSBundle mainBundle] URLForResource:#"test" withExtension:#"plist"]; //Lets get the file location
NSDictionary *plistContent = [NSDictionary dictionaryWithContentsOfURL:file];
NSLog(#"dic::%#", plistContent);
NSArray *arrayRead = [plistContent allKeys];
NSLog(#"test::%#", arrayRead);
Everything I seem to do returns (null) in my logs. Any ideas why?
Your plist root is an array, not a dictionary. Use:
NSArray *plistContent = [NSArray arrayWithContentsOfURL:file];
Of course you now have an array of dictionaries. You can't simply ask for allKeys anymore.

Read a plist from website

I am incorporating an image gallery into my project. It currently reads from a local plist file, and I need to change it to read from a web server. I've read a number of ways to do this, but can't make it work in my context. This seems maddeningly simple.
Below is my code:
NSString *plistPath =
[[NSBundle mainBundle] pathForResource:#"Images" ofType:#"plist"];
NSArray *imagePaths = [NSArray arrayWithContentsOfFile:plistPath];
You can view my plist here
This is what I've tried to no avail:
//get image URLs
NSURL *plistPath = [NSURL URLWithString:#"http://kpd.altervista.org/Images.plist"];
NSDictionary *imagePaths = [[NSDictionary alloc] initWithContentsOfURL:plistPath];
I'd greatly appreciate anyone's help.
Try this and it should be work :
NSURL *plistPath = [NSURL URLWithString:#"http://kpd.altervista.org/Images.plist"];
NSArray *imagePaths = [NSArray arrayWithContentsOfURL:plistPath];
NSLog(#"%#", imagePaths);
The URL you provide serves a plist that contains an array.
Your code tries to read it as a dictionary.
Just change your code to read it as an array and you'll be one step closer.
But as others have said in the comments, you can't download a file (plist or otherwise) to your bundles directory. You have to download it to your Documents directory or similar.

Resources