iOS - Issue with reading from a plist - ios

I'm having an issue with reading from a plist I can't seem to figure.
My plist list looks like this (note i've simplified it for examples sake):
Then I'm reading code like this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"ContactDetails" ofType:#"plist"];
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSArray* allmyKeys = [myDictionary allKeysForObject:#"Name1"];
Any ideas - the issue i keep facing is allmyKeys shows up as containing 0 objects. On debugging myDictionary is correctly populated so not sure why it doesn't work.
Many Thanks

The problem is with this line:
NSArray* allmyKeys = [myDictionary allKeysForObject:#"Name1"];
#"Name1" is a key in your dictionary, not a value. Even if it were a value, the string #"Name1" is a different object from the string with the same value in myDictionary, so this call would not do what you expect.
You probably want to access the dictionary by doing
NSDictionary *userDetails = myDictionary[#"Name1"];

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"]);
}];

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.

how to load plist data into array in IOS property list

NSString *path = [[NSBundle mainBundle] pathForResource:#"recipes" ofType:#"plist"]];
NSDictionary *dict = [[NSDictionary alloc] initwithContentOfFile:path];
NSArray *textData=[NSArray new];
textData = [dict objectForkey:#"TableData"];
textData is my array name,
recipes is plist name
after excuting my text data is being empty...
where is the mistake.
The problem is that you converting .plist to dictionary in the wrong way. Check dict property, it should be nil. Code to create NSDictionary from .plist file is listed below:
CFPropertyListRef plist = CFPropertyListCreateFromXMLData(kCFAllocatorDefault,
(__bridge CFDataRef)propertyListData,
kCFPropertyListImmutable, NULL);
NSDictionary *settings = (__bridge NSDictionary *)plist;
Try this:
textData = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"recipes" ofType:#"plist"]];
Anyways, you may can take a look of this tutorial:
http://ios-blog.co.uk/tutorials/how-to-populate-a-uitableview-from-a-plist-property-list/

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.

Create a new plist from the existing plist?

I have a plist like in the below first image ,thats my main plist and in that the field "bid" is unique . i have the value of bid (for example bid=90) then i need to search the value in all entries of my main (first image)plist and create a new plist& retrieve the corresponding values(bid,description,category,rating) belongs to that plist .How can i do this?
EDIT
if i have multiple values with me like bid =88 and another bid=90 then i have to create a new plist from the main plist (as shown in first image) then the result should be like image 3
NSString *path = [[NSBundle mainBundle] pathForResource:#"mainPlistFile" ofType:#"plist"];
//path has path for main plist.
NSDictionary *dictionary=[NSDictionary dictionaryWithContentOfFile: path];
NSArray *listbook = [dictionary objectForKey:#"listbook"];
for (NSDictionary *item in listbook) {
if ([[item objectForKey:#"bid"] isEqualToString yourNSStringWithBidToSearch]) {
//now item is the desired dictionary, you can get values by using objectForKey
//or write it to a file like below.
[item writeToFile:pathToSaveNewPlist automatically:NO];
break;
}
}
Remember you will have to first create the NSString pathToSaveNewPlist and yourNSStringWithBidToSearch, I'll leave that to you.
So you edited the question :
Lets assume you have an NSArray bidsToSearch which has NSStrings of the bid values to be searched :
NSMutableArray *targetListbook = [[NSMutableArrray alloc] init]];
NSDictionary *dictionary=[NSDictionary dictionaryWithContentOfFile:[[NSBundle mainBundle] pathForResource:#"mainPlistFile" ofType:#"plist"]];
for (NSDictionary *item in [dictionary objectForKey:#"listbook"])
if ([bidsToSearch containsObject:[item objectForKey:#"bid"]])
[targetListbook addObject:item];
[#{listbook:targetListbook} writeToFile:pathToSaveNewPlist automatically:NO];
You can use NSPropertyListSerialization class to serialize and deserialize property lists.
EDIT: below is some pseudo code for how to do it.
NSData* data = [NSData dataWithContentsOfFile:plistFile]; // read file
id plist = [NSPropertyListSerialization propertyListWithData:data ...]; // deserialize
// do your work
data = [NSPropertyListSerialization dataWithPropertyList:plist2 ...]; // serialize
[data writeToFile:plistFile2 ...]; // write to file

Resources