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"];
Related
I'm modifying someone elses code and struggling to access values within a plist, I can access Info.plist etc.. with code such as:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:#"Info.plist"];
NSDictionary *plistData = [NSDictionary dictionaryWithContentsOfFile:finalPath];
NSLog(#"ret=%#", plistData);
However when trying to access Account.plist, it returns null, I'm assuming it's a path issue?
Any help/pointers appreciated.
Here are examples of how to get either a Dictionary or an Array out of a plist.
//*******************************************
- (NSArray *)loadArrayFromPlist:(NSString *)plistName {
NSString *plistPath = [[NSBundle mainBundle] pathForResource: plistName ofType: #"plist"];
return [NSArray arrayWithContentsOfFile: plistPath];
}
//*******************************************
- (NSDictionary *)loadDictionaryFromPlist:(NSString *)plistName {
NSString *plistPath = [[NSBundle mainBundle] pathForResource: plistName ofType: #"plist"];
return [NSDictionary dictionaryWithContentsOfFile: plistPath];
}
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;
}
Got a little problem. I have a .plist file which contain next data
So i cant understand, how i need read first array, than in array read dictionary. Or maybe need rewrite file and change Root key to type dictionary, and read like this:
NSString *errorDesc = nil;
NSPropertyListFormat format;
NSString *plistPath;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) objectAtIndex:0];
plistPath = [rootPath stringByAppendingPathComponent:#"Data.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
plistPath = [[NSBundle mainBundle] pathForResource:#"Data" ofType:#"plist"];
}
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
propertyListFromData:plistXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format
errorDescription:&errorDesc];
if (!temp) {
NSLog(#"Error reading plist: %#, format: %d", errorDesc, format);
}
self.personName = [temp objectForKey:#"Name"];
self.phoneNumbers = [NSMutableArray arrayWithArray:[temp objectForKey:#"Phones"]];
You can do it by the following code:
NSString *plistPath = [[NSBundle mainBundle] pathForResource:#"Data" ofType:#"plist"];
NSArray *dataArray = [NSArray arrayWithContentsOfFile:plistPath];
This array will contain all the dictionaries, you can get them like:
NSDictionary *dict = [dataArray objectAtIndex:0];
SString *plistFile = [[NSBundle mainBundle] pathForResource:#"Data" ofType:#"plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:plistFile];
for(NSDictionary *dictionary in array) {
NSLog(#"%#", dictionary);
}
NSDictionary *item0 = array[0];
NSString *imageName = item[0][#"name"];
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 have a plist named as "config.plist" in xcode ,i tried to retrieve register url (key :reg ) but when i print that result it showing null value?
This is my code to retrieve string ..please refer image for my plist structure.
NSMutableURLRequest *request =[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString: [[_config objectForKey:#"URL"] objectForKey:#"reg"]]];
NSString *plist=[[NSBundle mainBundle] pathForResource:#"config" ofType:#"plist"];
NSDictionary *dict=[[NSDictionary alloc] initWithContentsOfFile:plist];
NSString *stringValue=dict[#"URL"][#"reg"];
EDIT: Very Important to understand the following:
In the above dict itself is the objectForKey:#"Root"
And I/we trying to find by [#"Root"][#"URL"][#"reg"] which is not there.
So for readability with namingConventions(what it stores) it should be :
NSString *plist=[[NSBundle mainBundle] pathForResource:#"config" ofType:#"plist"];
NSDictionary *rootDict=[[NSDictionary alloc] initWithContentsOfFile:plist];
NSString *stringValue=dict[#"URL"][#"reg"];
Or, even
NSString *plist=[[NSBundle mainBundle] pathForResource:#"config" ofType:#"plist"];
NSString *stringValue=[[NSDictionary alloc] initWithContentsOfFile:plist][#"URL"][#"reg"];
Try
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:#"config.plist"];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:path];
NSString *result = [[[dictionary objectForKey:#"Root"] objectForKey:#"URL"]objectForKey:#"reg"];
Edit for Resource:
NSString *path = [[NSBundle mainBundle] pathForResource:#"config" ofType:#"plist"];
Try This Code
NSString *plistFilePath=[[NSBundle mainBundle] pathForResource:#"config" ofType:#"plist"];
NSDictionary *urlDictionary=[[NSDictionary alloc] initWithContentsOfFile:plistFilePath];
NSString *urlString=[[urlDictionary objectForKey:#"URL"] valueForKey:#"reg"];
NSString *plist=[[NSBundle mainBundle] pathForResource:#"config" ofType:#"plist"];
NSLog(#"path%#",plist);
NSDictionary *dict=[[NSDictionary alloc] initWithContentsOfFile:plist];
NSLog(#"d%#",dict);
NSString *stringValue=[[dict objectForKey:#"URL"] valueForKey:#"reg"];
This thing works for me..Thanks for your all help