I have a plist which is an array, the file name is called startups.plist. I tried to read it into an array by the following code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"startups" ofType:#"plist"];
NSMutableArray * wordList = [[NSMutableArray alloc] initWithContentsOfFile:path];
However, wordList is always 0 here. Printed the path when testing on device, and it gives me:
/var/mobile/Applications/CCBF94D9-389C-4D63-B023-F39653FDCEF4/My.app/startups.plist
Here's what the structure looks like of the plist:
the size of the array is 0.
What am I doing wrong here?
initWithContentsOfFile: will not open plist file. Here is a sample:
NSString *errorDesc = nil;
NSPropertyListFormat format;
NSString *path = [[NSBundle mainBundle] pathForResource:#"startups" ofType:#"plist"];
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:path];
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
propertyListFromData:plistXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format
errorDescription:&errorDesc];
NSLog(#"%#",[temp objectForKey:#"Root"]);
You have to use NSDictionary since your plist is a Dictionary which has a key Root which is an array.
NSString *path = [[NSBundle mainBundle] pathForResource:#"startups" ofType:#"plist"];
NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSMutableArray *wordList = (NSMutableArray *)[dict objectForKey:#"Root"];
I'm not sure about casting to NSMutableArray. Maybe you have to do
NSMutableArray *wordList = [NSMutableArray arrayWithArray:(NSArray*)[dict objectForKey:#"Root"]]
Related
Now i am currently working on pdf reader application, i can read all the pdf files which are present inside the application by using the following code,
NSArray *userDocuments = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSURL *docementsURL = [NSURL fileURLWithPath:[userDocuments lastObject]];
NSArray *documentsURLs = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:docementsURL
includingPropertiesForKeys:nil
options:NSDirectoryEnumerationSkipsHiddenFiles
error:nil];
NSMutableArray *names = [NSMutableArray array];
NSMutableDictionary *urls = [NSMutableDictionary dictionary];
NSArray *bundledResources = [[NSBundle mainBundle] URLsForResourcesWithExtension:#"pdf" subdirectory:nil];
documentsURLs = [documentsURLs arrayByAddingObjectsFromArray:bundledResources];
for (NSURL *docURL in documentsURLs)
{
NSString *title = [[docURL lastPathComponent] stringByDeletingPathExtension];
[names addObject:title];
[urls setObject:docURL forKey:title];
}
documents = [[NSArray alloc] initWithArray:[names sortedArrayUsingSelector:#selector(compare:)]];
urlsByName = [[NSDictionary alloc] initWithDictionary:urls];
But my problem is to read the pdf file folder by folder and store that in to separate arrays,
my folder structure is like the following image,
Any help regarding this will be appreciated..
For this purpose you can create bundles instead of folders
and then get all bundles and files inside them
something like this
NSArray *bundleArr = [[NSBundle mainBundle]pathsForResourcesOfType:#"bundle" inDirectory:nil];
NSLog(#"pdfs in bundle %# is my class is %#",[bundleArr objectAtIndex:0],[[bundleArr objectAtIndex:0]class]);
for (int i=0; i<[bundleArr count]; i++) {
NSString *myBundleStr=[bundleArr objectAtIndex:i];
NSBundle *myBundle = [[NSBundle alloc]initWithPath:[bundleArr objectAtIndex:i]];
NSArray *pdfPaths = [myBundle pathsForResourcesOfType:#"pdf" inDirectory:nil];
NSLog(#"\n\nPDF in bundle is %#",pdfPaths);
}
I have generated a property list which save some hospital names. Now I want to get the names into an array from that plist. please help me. Thanx in advance.
NSString* plistPath = [[NSBundle mainBundle] pathForResource:#"sample" ofType:#"plist"];
contentArray = [NSArray arrayWithContentsOfFile:plistPath];
try this
Try this
NSMutableDictionary* dictionary = [[[NSMutableDictionary alloc] init] autorelease];
NSError *error;
// Look in Documents for an existing plist file
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* metadataPlistPath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat: #"Metadata.plist"]];
[metadataPlistPath retain];
// If it's not there, copy it from the bundle
NSFileManager *fileManger = [NSFileManager defaultManager];
if ( ![fileManger fileExistsAtPath:metadataPlistPath] ) {
NSString *pathToSettingsInBundle = [[NSBundle mainBundle]
pathForResource:#"Metadata" ofType:#"plist"];
[fileManger copyItemAtPath:pathToSettingsInBundle toPath:metadataPlistPath error:&error];
}
//Now read the plist file from Documents
NSString* myPlistPath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat: #"Metadata.plist"] ];
dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:myPlistPath];
return dictionary;
}
//fetch the data based on key
- (NSMutableArray*) fetchMetadataArrayForKey:(NSString*)aKey {
NSMutableArray* arrCategories = [[[NSMutableArray alloc] init] autorelease];
NSMutableDictionary* dictionary = [self fetchMetadata];
arrCategories = [dictionary valueForKey:aKey];
return arrCategories;
}
I am new to iOS development, and I would like to know how .plist files work in iOS. Can any one help me by giving me an example of how to read an write data to and from a .plist?
Yup:
NSString *rootPath = [[NSBundle mainBundle] bundlePath];
NSString *pListPath = [rootPath stringByAppendingPathComponent:#"Settings.bundle/Root.plist"];
NSDictionary *pList = [NSDictionary dictionaryWithContentsOfFile:pListPath];
Writing to a file isn't too hard either:
NSDictionary *dict;
NSData *data = [NSPropertyListSerialization dataWithPropertyList:dict format:NSPropertyListXMLFormat_v1_0 options:0 error:&error];
[data writeToFile:self.saveToPath atomically:YES];
A really quickly trying to add a key to a .plist. I almost have it, what is the correct version of the fourth line?
NSString *path = [[NSBundle mainBundle] pathForResource:#"Favourites" ofType:#"plist"];
NSDictionary *rootDict = [[NSDictionary alloc] initWithContentsOfFile:path];
[rootDict addKey:#"Test"]; //guessed code
[rootDict writeToFile:path atomically: YES];
You can't add elements to a NSDictionary, you need to use a NSMutableDictionary then use the setObject:forKey: method.
[rootDict setObject:someObject forKey:#"someKey"];
See NSMutableDictionary class reference
almost have it
not really.
You can't change a NSDictionary. You can't write to the mainbundle.
working code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"Favourites" ofType:#"plist"];
NSMutableDictionary *rootDict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
[rootDict setObject:#"Test" forKey:#"Key"];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:#"Favourites.plist"];
[rootDict writeToFile:writablePath atomically: YES];
[rootDict release];
I am new to iPhone development. I am using the below code to add the value to the plist but when check the plist after executing the code I don't see any value saved in the plist. Where did I go wrong? The plist I have created is in the resource folder.
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:#"regvalue.plist"];
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:finalPath];
[plistDict setValue:#"hello" forKey:#"choice"];
[plistDict writeToFile:finalPath atomically: YES];
For retrieving the value
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:#"regvalue.plist"];
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:finalPath];
NSString *value;
value = [plistDict objectForKey:#"choice"];
NSLog(#"the value is %#",value);
It gives only null value.
The resources folder is not writeable. You should save it into the documents folder.
Maybe you can find the answer here:
How to write data in plist?
But if you only want save some message in you app, NSUserDefaults is the better way.