How can I retrieve string from plist? - ios

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

Related

Objective-c accessing values in plist

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

reading plist with nsbundle, in one case it works and in an almost similar case it doesn't

Could someone tell me why this code works:
..non-relevant previous code
[self readPlist:#"s_a.plist"];
}
}
- (void)readPlist: (NSString *)fileName{
NSString *bundleString = [[NSBundle mainBundle] bundlePath];
NSString *plistPath = [bundleString stringByAppendingPathComponent:fileName];
NSArray *arr = [NSArray arrayWithContentsOfFile:plistPath];
But the following returns nil?
[self readPlist:#"s_a.plist"];
}
}
- (void)readPlist: (NSString *)fileName{
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:fileName ofType:#"plist"];
NSArray *arr = [NSArray arrayWithContentsOfFile:plistPath];
To me it seems that I'm doing the same thing. I've been looking at several SO solutions, and I just can't figure it out.
[self readPlist:#"s_a.plist"];
NSString *plistPath = [bundle pathForResource:fileName ofType:#"plist"];
you are passing s_al.plist as fileName so the method now looks for s_a.plist.plist which it fails

Displaying plist based on variable

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

Dictionary with contents of file not working in ios

I am trying to get a dictionary from a plist in ios.The problem is the code is not working.It works in a different function but not this one.
NSDictionary* pdata=[NSDictionary dictionaryWithContentsOfFile:datapath];
The file exists at path but for some reason its not working.I get null when I try to print the dictionary.
EDIT: Here is my path/private/var/mobile/Applications/F4D5EFE8-58FC-4179-9492-4C4F9FD01024/Library/Preferences/abc.def.ghi.plist.This is a jailbroken application and I was able to do this successfully for another pplist in a seperate location.
This Should Work Fine.
NSString *plistFileUrl=[[NSBundle mainBundle] pathForResource:#"someplistFile" ofType:#"plist"];
NSURL *url=[NSURL fileURLWithPath:plistFileUrl];
NSDictionary *aDict=[[NSDictionary alloc] initWithContentsOfURL:url];
This must work for you.
NSString *aStr=[[NSBundle mainBundle] pathForResource:#"Property List" ofType:#"plist"];
NSURL *aUrl=[NSURL fileURLWithPath:aStr];
NSDictionary *aDict=[[NSDictionary alloc] initWithContentsOfURL:aUrl];
Try this .
NSDictionary* dict = [NSDictionary dictionaryWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"example" ofType:#"plist"]isDirectory:NO]];
NSLog(#"data is %#",dict);
Try this
//Local mainbundle
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *filePath = [path stringByAppendingPathComponent:#"Contents.plist"];
NSMutableDictionary *dictContent = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
NSLog(#"%#",dictContent);
///DocumentsDirectory
NSMutableDictionary *dictItem=[[NSMutableDictionary alloc]init];
[dictItem setObject:#"ravi" forKey:#"name"];
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *docFilePath = [documentsDirectoryPath
stringByAppendingPathComponent:#"data.plist"];
[dictItem writeToFile:docFilePath atomically: YES];
NSMutableDictionary *dictContent1 = [NSMutableDictionary dictionaryWithContentsOfFile:docFilePath];
NSLog(#"%#",dictContent1);

How to add the information in NSMutableDictionary from two plist?

I have two plist. How to add the information in NSMutableDictionary from two plist? how to do it from a plist, I know. But I have 2 plist.
NSString *path = [[NSBundle mainBundle] pathForResource:
#"Eur" ofType:#"plist"];
NSString *path1 = [[NSBundle mainBundle] pathForResource:
#"Asia" ofType:#"plist"];
stationDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
I need add in stationDictionary two plist: 1 Eur 2 Asia. But I do not know how to add the information from the second plist?
Thanks!
NSString *path = [[NSBundle mainBundle] pathForResource:
#"Eur" ofType:#"plist"];
NSString *path1 = [[NSBundle mainBundle] pathForResource:
#"Asia" ofType:#"plist"];
NSMutableDictionary* stationDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSDictionary* dicForPath1 = [[NSDictionary alloc] initWithContentsOfFile:path1];
[stationDictionary addEntriesFromDictionary:dicForPath1];

Resources