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];
}
Related
I have the structure like the following in my Xcode project.
I need to read all the images from project and need to store it in array.
I have used the following code from some other StackOverflow posts, but it's not working.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"Images"ofType:#""];
NSDirectoryEnumerator *direnum;
direnum = [fileManager enumeratorAtPath: filePath];
imageFolder = [NSMutableArray new];
for(NSString *filename in direnum){
if([[filename pathExtension] isEqualToString:#"png"]){
[imageFolder addObject:filename];
}
}
NSLog(#"Files in the folder %#",imageFolder);
I would appreciate, if any one help me to complete this.
it's simple to get all the images into a array
NSBundle *imageBundle=[NSBundle bundleWithPath: myBundlePath];
NSArray *allImageURLs=[imageBundle URLsForResourcesWithExtension:#"png" subdirectory:nil];
- (NSArray *)pathsForResourcesOfType:(NSString *)extension inDirectory:(NSString *)subpath will get you the array you want.
NSArray *imagesArray = [[NSBundle mainBundle] pathsForResourcesOfType:#"png" inDirectory:#"Images"];
You can try below
NSArray *pathArray = [[NSBundle mainBundle] pathsForResourcesOfType:#"png" inDirectory:nil];
[pathArray enumerateObjectsUsingBlock:^(NSString *pathString, NSUInteger idx, BOOL *stop) {
UIImage *yourImage = [[UIImage alloc] initWithContentsOfFile:pathString];
}];
Please use this code, hope it will work.
NSString *dirPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:#"images"];
NSError * error;
NSArray * images = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath:dirPath error:&error];
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
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);
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];
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