I am trying to get the child element SHOP from a plist. The plist is as so :
Settings is a section.
I tried using the following but not sure how to put it in a cellForIndexPath
NSDictionary *region = [self.dict valueForKey:#"Settings"];
NSString *name = [region valueForKey:#"Shop"];
NSLog(#"%#", name);
My Plist is as so:
NSString *path = [[NSBundle mainBundle] pathForResource:#"PropertyList" ofType:#"plist"];
NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.dict = tempDict;
NSArray *tempArray = [[NSArray alloc] init];
self.namesArray = tempArray;
self.sectionArray = [self.dict allKeys];
If what you need is to read the "Shop" array from the "item 0" dictionary you need to do the following:
NSString *path = [[NSBundle mainBundle] pathForResource:#"PropertyList" ofType:#"plist"];
NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.dict = tempDict;
NSArray *settingsArray = self.dict[#"Settings"];
NSDictionary *itemDict = settingsArray[0];
NSArray *shopArray = itemDict[#"Shop"];
Of course you should check that the keys and indexes you're using are valid before indexing to avoid crashes.
Related
I want to create plist file with multiple array and array as Root type.I can add multiple array in dictionary but I want to add all array in one array programmatically.How is it possible to create plist file programmatically and write array data into it.Please guide me and give some sample links.Thanks.
Here is my code to add array in dictionary :
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"Demo.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:#"Demo" ofType:#"plist"];
[fileManager copyItemAtPath:bundle toPath: path error:&error];
}
firstArray = [[NSMutableArray alloc] initWithObjects:#"15-5-123",#"15-5-12",nil];
secondArray = [[NSMutableArray alloc] initWithObjects:#"TestFiling",#"TestFiling",nil];
thirdArray = [[NSMutableArray alloc] initWithObjects:#"15132561",#"15135601", nil];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObject:firstArray forKey:#"firstArray"];
[dictionary setObject:secondArray forKey:#"secondArray"];
[dictionary setObject:thirdArray forKey:#"thirdArray"];
[dictionary writeToFile:path atomically:NO];
NSDictionary *dictionary1;
dictionary1 = [NSDictionary dictionaryWithContentsOfFile:path];
NSLog(#"dictionary1 %#",dictionary1);
this code is working fine for dictionary but i want to add arrays in array.
The first section of your code is fine, you just need to switch the second part to using an array:
NSMutableArray *array = [[NSMutableDictionary alloc] init];
[array addObject:firstArray];
[array addObject:secondArray];
[array addObject:thirdArray];
[array writeToFile:path atomically:NO];
NSArray *array1 = [NSArray arrayWithContentsOfFile:path];
NSLog(#"array1 %#", array1);
I am downloading a key from plist to my array on button click, like this:
- (IBAction)nextKey:(id)sender {
NSString *mainBundlePath = [[NSBundle mainBundle] bundlePath];
NSString *plistPath = [mainBundlePath stringByAppendingPathComponent:#"text.plist"];
NSDictionary *dict = [[NSDictionary new] initWithContentsOfFile:plistPath];
wordsArray = [dict objectForKey:#"key1"];
}
I need change key value to the next from plist on each button click.
How i can do this?
Add an NSArray in your class. And keyvalue in it.Also add a int variable.Like
NSArray *arrKey = [[NSArray alloc] initWithObjects:#"Key1",#"Key2",#"Key3",#"Key4",#"Key5", nil];
int flagKey = 0;
- (IBAction)nextKey:(id)sender {
NSString *mainBundlePath = [[NSBundle mainBundle] bundlePath];
NSString *plistPath = [mainBundlePath stringByAppendingPathComponent:#"text.plist"];
NSDictionary *dict = [[NSDictionary new] initWithContentsOfFile:plistPath];
wordsArray = [dict objectForKey:[arrKey objectAtIndex:flagKey]];
if(flagKey < arrKey.count-1){
flagkey++;
}
}
You can maintain a count and each time when button pressed , increase the count like
-(IBAction)nextKey:(id)sender {
NSString *mainBundlePath = [[NSBundle mainBundle] bundlePath];
NSString *plistPath = [mainBundlePath stringByAppendingPathComponent:#"text.plist"];
NSDictionary *dict = [[NSDictionary new] initWithContentsOfFile:plistPath];
wordsArray = [dict objectForKey:[NSString stringWithFormat:#"key%d",index]];
index++;
}
Or you can set the button tag, and alter it every time when you press Button. Set initially it to 0 or starting key name in your plist file
-(IBAction)nextKey:(id)sender {
UIButton *button = (UIButton *)sender;
NSString *mainBundlePath = [[NSBundle mainBundle] bundlePath];
NSString *plistPath = [mainBundlePath stringByAppendingPathComponent:#"text.plist"];
NSDictionary *dict = [[NSDictionary new] initWithContentsOfFile:plistPath];
wordsArray = [dict objectForKey:[NSString stringWithFormat:#"key%d",button.tag]];
button.tag = button.tag + 1;
}
I am currently displaying my plist using the code below, which works, however this seems inefficient to me and I presume there is a cleaner approach.
if ([self.title isEqual: #"How to wear a Kilt"]){
NSString *path = [[NSBundle mainBundle] pathForResource:#"wearAKilt" ofType:#"plist"];
// Load the file content and read the data into arrays
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
thumbImg = [dict objectForKey:#"thumbImg"];
stepLabel = [dict objectForKey:#"stepLabel"];
descLabel = [dict objectForKey:#"descLabel"];
}
// Find out the path of recipes.plist
else if ([self.title isEqual: #"How to tie a cravat"]){
NSString *path = [[NSBundle mainBundle] pathForResource:#"wearACravat" ofType:#"plist"];
// Load the file content and read the data into arrays
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
thumbImg = [dict objectForKey:#"thumbImg"];
stepLabel = [dict objectForKey:#"stepLabel"];
descLabel = [dict objectForKey:#"descLabel"];
}
else if ([self.title isEqual: #"How to wear a sporran"]){
NSString *path = [[NSBundle mainBundle] pathForResource:#"wearASporran" ofType:#"plist"];
// Load the file content and read the data into arrays
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
thumbImg = [dict objectForKey:#"thumbImg"];
stepLabel = [dict objectForKey:#"stepLabel"];
descLabel = [dict objectForKey:#"descLabel"];
}
I have tried using a if statement just on the *path but this (as expected) produces a undeclared identifier path error.
if ([self.title isEqual: #"How to wear a Kilt"]){
NSString *path = [[NSBundle mainBundle] pathForResource:#"wearAKilt" ofType:#"plist"];
} else if ([self.title isEqual: #"How to tie a cravat"]) {
NSString *path = [[NSBundle mainBundle] pathForResource:#"wearACravat" ofType:#"plist"];
}
Any suggestions?
You second bit of code, just do:
NSString *path;
if ([self.title isEqual: #"How to wear a Kilt"]){
path = [[NSBundle mainBundle] pathForResource:#"wearAKilt" ofType:#"plist"];
} else if ([self.title isEqual: #"How to tie a cravat"]) {
path = [[NSBundle mainBundle] pathForResource:#"wearACravat" ofType:#"plist"];
}
You could also use a dictionary to map from the incoming string to the associated file name...
e.g.
NSDicttionary *mapping = #{
#"How to wear a Kilt" : #"wearAKilt",
#"How to tie a cravat" : #"wearACravat",
};
Which could be a static definition, and should probably consider localisation. Then:
NSString *name = [mapping objectForKey:self.title];
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:#"plist"];
I am having trouble reading the data in a plist file. I'm not getting the titleString printed in the console like I'm expecting. What am I doing wrong?
NSString *path = [[NSBundle mainBundle] pathForResource:#"Events" ofType:#"plist"];
NSDictionary *dictPri = [[NSMutableDictionary alloc]initWithContentsOfFile:path];
NSMutableArray *arrEvents = [[NSMutableArray alloc] initWithArray:[dictPri valueForKey:#"Root"]];
for (NSDictionary *dict in arrEvents)
{
NSString *titleString = nil;
NSString *date = nil;
titleString = [NSString stringWithFormat:#"%#",[dict valueForKey:#"Title"]];
date = [NSString stringWithFormat:#"%#",[dict valueForKey:#"Date"]];
NSLog(#"Title String: %#", titleString);
}
Your main element (Root) is Dictionary - not Array - change it in plist by clicking on type next to it.
Also there is a problem in your code - you never access "Root" element by name - it's by default top-level object. Consider taking out additional array initialization which is not required.
Fixed code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"Events" ofType:#"plist"];
NSArray* arrEvents = [NSArray arrayWithContentsOfFile:path];
for (NSDictionary *dict in arrEvents)
{
NSString *titleString = [NSString stringWithFormat:#"%#",[dict objectForKey:#"Title"]];
NSString *date = [NSString stringWithFormat:#"%#",[dict objectForKey:#"Date"]];
NSLog(#"Title String: %#", titleString);
}
I would like to access coordinate values which are latitude and longitude.
NSString *path = [[NSBundle mainBundle] pathForResource:
#"WellList" ofType:#"plist"];
NSMutableArray *cities = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSDictionary *routes =[[NSDictionary alloc]initWithContentsOfFile:path];
NSDictionary *city =[[NSDictionary alloc]init];
NSDictionary *locations =[[NSDictionary alloc]init];
NSDictionary *locationInfo=[[NSDictionary alloc]init];
NSMutableArray *localCities= [[NSMutableArray alloc]init];
city= routes [#"Routes"];
NSLog (#"%#",city);
localCities =[[NSMutableArray alloc] initWithArray:[city allValues]];
locations =[localCities objectAtIndex:1];
I think your problem is that you don't understand how loading a .plist file works. Routes and Cities are being initialized to the same value, because you are loading both of them from the same file. So to access the latitude of Houston's Location 1, you would write
NSMutableDictionary *root = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSString* latitude = [[[[[root objectForKey: #"Routes"] objectForKey: #"Houston" ] objectForKey: #"Location 1"] objectForKey: #"coordinate"] objectForKey: #"latitude"];