I have a plist containing an array with three elements all of which are dictionaries. These dictionaries contain four items each (latitude, longitude, title, subtitle). I want to loop through each of the elements and get the latitude and longitude (both of which are attributes of the dictionary).
The following code is used.
- (void)loadAnnotations{
//retrieve path of plist file and populate relevant types with its information
NSString *plistPath = [[NSBundle mainBundle] pathForResource:#"Places1" ofType:#"plist"];
branchList = [[NSArray alloc] initWithContentsOfFile:plistPath];
NSLog(#"hi inside method",branchList.lastObject);
self.branchList =[NSMutableArray array];
//code ok until this
for (NSDictionary *key in branchList)
{ NSLog(#"hi in loop");
PlaceFinderAnnotation *placeAnnotations = [[PlaceFinderAnnotation alloc] init];
//loop through annotations array, creating parking annotations filled with the information found in the plist
CLLocationDegrees latitude = [[key valueForKey:#"latitude"]floatValue];
CLLocationDegrees longitude = [[key valueForKey:#"longitude"]floatValue];
placeAnnotations.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
[self.branchList addObject:placeAnnotations];
[placeAnnotations release]; placeAnnotations= nil;
objectForKey:#"subtitle"]];
}
}
The problem is it doesnt go into the loop. meaning it doesnt print out the log command "hi inside the looppp".
Let's assume this code succeeded (we have to assume because you don't appear to be checking to make sure). Let's also assume (because you don't say) "branchList" is an instance variable in the current class:
NSString *plistPath = [[NSBundle mainBundle] pathForResource:#"Places1" ofType:#"plist"];
branchList = [[NSArray alloc] initWithContentsOfFile:plistPath];
This hopefully leaves you with an array. You could of course eliminate the "hopefully" by ... checking to make sure it leaves you with an array ( if (branchList)... ). Then, since "branchList" seems to be an instance variable, you immediately blow it away by replacing it with an empty array (using an accessor rather than setting it directly as you did above):
self.branchList =[NSMutableArray array];
...so then you try to iterate an empty loop (so the NSLog() statement is never executed).
self.branchList =[NSMutableArray array];
creates an empty array and that is what the for statement is asked to loop through.
Delete that statement.
Perhaps this is what you want:
NSString *plistPath = [[NSBundle mainBundle] pathForResource:#"Places1" ofType:#"plist"];
self.branchList = [NSArray arrayWithContentsOfFile:plistPath];
NSLog(#"hi inside method",branchList.lastObject);
for (NSDictionary *key in self.branchList) {
NSLog(#"hi inside loopppp");
if([branchlist count]){
for (NSDictionary *key in self.branchList) {
NSLog(#"Key item in branchlist %#",key);
}
}else{
NSLog(#"There is no items in branchlist");
}
Related
I am studying iPhone development and facing a problem with a reading/writing plist file. I followed an example from a iPhone development book but keep getting an error message when running.
The error message says : 2012-04-26 00:21:09.759 FileHandling[5915:207] -[__NSCFDictionary addObject:]: unrecognized selector sent to instance 0x685ac40
Here is the example code (it seems fine to me...though):
NSString *plistFileName = [[self documentPath] stringByAppendingPathComponent: #"Apps.plist"];
NSLog(#"Where is the file? => %#", plistFileName);
if ([[NSFileManager defaultManager] fileExistsAtPath:plistFileName]) {
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistFileName];
for (NSString *category in dict) {
NSLog(#"%#", category);
NSLog(#"=========");
NSArray *titles = [dict valueForKey:category];
for (NSString *title in titles) {
NSLog(#"%#", title);
}
}
} else {
NSString *plistPath = [[NSBundle mainBundle] pathForResource:#"Apps" ofType: #"plist"];
NSLog(#"%#", plistPath);
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile: plistPath];
NSLog(#"Let's take a look : %#", dict);
NSMutableDictionary *copyOfDict = [dict mutableCopy];
NSLog(#"Let's look at the mutable dictationary : %#", copyOfDict);
NSArray *categoriesArray = [[copyOfDict allKeys] sortedArrayUsingSelector: #selector(compare:)];
for (NSString *cateogry in categoriesArray) {
NSArray *titles = [dict valueForKey: cateogry];
NSMutableArray *mutableTitles = [titles mutableCopy];
[mutableTitles addObject: #"New App Title"];
[copyOfDict setObject: mutableTitles forKey:cateogry];
}
NSString *fileName = [[self documentPath] stringByAppendingPathComponent: #"Apps.plist"];
[copyOfDict writeToFile: fileName atomically:YES];
}
According to the error message, the problem is occurring in the call to addObject: on an __NSCFDictionary. This means that, at runtime, a dictionary received a message to add an object.
However, in this code snippet, addObject: is apparently being sent to an NSMutableArray. This probably means that each object titles you're retrieving from dict in the last for-loop is not an array, but in fact another dictionary, that your code is simply referring to as an array.
Indeed, your code does seem well-formed, so check the well-formedness of your source plist; open it up in a plain text editor. Also, you use a ton of logging, so confirm this way: in the output, dictionaries (including the root entry) are denoted by {curly = braces}, where arrays are denoted by (round parentheses).
I have two plist files that I'm using as datasources to create NSArray and NSDictionaries in my app.
I want the output of the CSV file to look like:
exerciseName, muscleGroup, description
Barbell Curl, Biceps, This is a bicep exercise
The problem is, I need to first combine two NSDictionaries I have. One has exerciseName and muscleName, while the other has exerciseDescription. But I need to have one array of dictionaries for each exercise obejct that has all 3 keys.
I'm using the following code to build the main NSMutableArray
if (muscleArray == nil)
{
NSString *path = [[NSBundle mainBundle]pathForResource:#"data" ofType:#"plist"];
NSMutableArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.muscleArray = rootLevel;
}
NSMutableArray *arrayForSearching = [NSMutableArray array];
for (NSDictionary *muscleDict in self.muscleArray)
for (NSDictionary *excerciseDict in [muscleDict objectForKey:#"exercises"])
[arrayForSearching addObject:[NSDictionary dictionaryWithObjectsAndKeys:
[excerciseDict objectForKey:#"exerciseName"], #"exerciseName",
[muscleDict objectForKey:#"muscleName"], #"muscleName", nil]];
self.exerciseArray = arrayForSearching;
I'm using the following code to build the NSDictionary which has the exerciseDescription key
NSString *exerciseNameString =self.exerciseName;
NSString *path = [[NSBundle mainBundle] pathForResource:#"ExerciseDescriptions" ofType:#"plist"];
NSDictionary *exerciseDescription = [NSDictionary dictionaryWithContentsOfFile:path];
NSString *description = [exerciseDescription objectForKey:exerciseNameString];
You obviously know how to iterate through and create dictionaries, so what's the problem?
Iterate through exerciseArray and add an exercise description to each of its dictionaries. However this seems wildly inefficient and you should probably rethink the whole thing. Why not store them on disk in a ready-to-use format?
Arrays are used when the order of items matters. It doesn't seems to really matter here, so why not just keep them as NSDictionaries? If you need to access all keys in a dictionary just call allKeys
EDIT: Like this?
NSString *path = [[NSBundle mainBundle] pathForResource:#"ExerciseDescriptions"
ofType:#"plist"];
NSDictionary *descriptions = [NSDictionary dictionaryWithContentsOfFile:path];
NSMutableArray *exercises = self.exerciseArray;
for (NSInteger i = 0; i < [exercises count]; i++) {
NSMutableDictionary *dict = [[exercises objectAtIndex:i] mutableCopy];
NSString *name = [dict valueForKey:#"exerciseName"];
NSString *desc = [descriptions valueForKey:name];
if (desc) {
[dict setValue:desc forKey:#"exerciseDescription"];
[exercises replaceObjectAtIndex:i withObject:dict];
}
[dict release];
}
I created the following project to address my need to merge two dictionaries created from plists:
https://github.com/bumboarder6/NSDictionary-merge
It works even if you have some duplicate entries between your two dictionaries or arrays and it also recursively merges so you get a merge of your whole plist even when it contains dictionaries of dictionaries of arrays of dictionaries (etc.).
I am having troubles with my class which reads and writes data to a plist. Here is some code:
This first chunk is from my custom class with all my plist read and write methods.
-(NSString *) dataFilePath{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
return [documentDirectory stringByAppendingPathComponent:#"userInformation.plist"];
}
-(bool)readUserIsMale{
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSDictionary *boolDict = [[NSDictionary alloc] initWithContentsOfFile:[self dataFilePath]];
return [[boolDict objectForKey:#"boolUserIsMale"] boolValue];
}
return nil;
}
-(void)writeUserIsMale:(bool)boolValue{
NSDictionary *boolDict = [[NSDictionary alloc] init];
[boolDict setValue:[NSNumber numberWithBool:boolValue] forKey:#"boolUserIsMale"];
[boolDict writeToFile:[self dataFilePath] atomically:YES];
}
I then in another class where desired import, create and use the class methods:
#import "plistReadWrite.h"
plistReadWrite *readWrite;
If I try and see its value in the console I get (null) return.
NSLog(#"%#",[readWrite readUserIsMale]);
This is of course after I have written some data like so:
[readWrite writeUserIsMale:isUserMale];
isUserMale being a bool value.
Any help would be massively appreciated, if you need anymore info let me know. Thanks.
I think this is mostly correct. In your writeUserIsMale: method you want a mutable dictionary, so you can actually set that key (this should have crashed for you as is, so I'm guessing a copy/paste problem?)
//NSDictionary *boolDict = [[NSDictionary alloc] init];
//should be:
NSMutableDictionary *boolDict = [[NSMutableDictionary alloc] init];
And then when you log the value, remember that bool (or BOOL) are primitives, not objects so:
NSLog (#"%d",[readWrite readUserIsMale]); // Will print 0 or 1
// or if you prefer:
NSLog (#"%#", ([readWrite readUserIsMale]? #"YES":#"NO")); // print YES or NO
Lastly, since this is objective-c, I would probably use BOOL instead of bool.
I'm assuming this is just a simple example, and that you know about NSUserDefaults for this sort of thing.
Hope that helps.
My app ships with a .plist that looks like this:
I want the user to be able to add a custom exerciseName.
So I need to create a new .plist in the user's document folder that mimics this format. Can anyone help me with this?
I need something like this (pseudo code)
if (userData == nil)
{
then create the .plist file;
setup the .plist to mimic the format of the img above.
}
now save exerciseName appropriately.
Update:
if (exerciseArray == nil)
{
NSString *path = [[NSBundle mainBundle]pathForResource:#"data" ofType:#"plist"];
NSMutableArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.exerciseArray = rootLevel;
[rootLevel release];
}
What you will want to do is load the Plist into an NSDictionary, and encode that NSDictionary back to a Plist file in your applications document folder. In your applicationDidFinishLoading: method, I would do something like this:
NSString * documentFile = [NSHomeDirectory() stringByAppendingFormat:#"/Documents/myPlist.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:documentFile]) {
// create a copy of our resource
NSString * resPath = [[NSBundle mainBundle] pathForResource:#"myPlist" ofType:#"plist"];
// NOTE: replace #"myPlist" with the name of the file in your Resources folder.
NSDictionary * dictionary = [[NSDictionary alloc] initWithContentsOfFile:resPath];
[dictionary writeToFile:documentFile atomically:YES];
[dictionary release];
}
Then, when you want to add an item, you want to use an NSMutableDictionary to modify and save the existing plist in the app's documents directory:
- (void)addExercise {
NSMutableDictionary * changeMe = [[NSMutableDictionary alloc] initWithContentsOfFile:documentFile];
... make changes ...
[changeMe writeToFile:documentFile atomically:YES];
[changeMe release];
}
To make changes, you will need to find the sub-dictionary containing the array of exercises. Then use the setObject:forKey: method on the NSMutableDictionary to set a new array containing a new list of exercises. This might look something like this:
NSMutableArray * list = [NSMutableArray arrayWithArray:[changeMe objectForKey:#"list"]];
NSArray * exercises = [[list objectAtIndex:10] objectForKey:#"exercises"];
NSDictionary * newExercise = [NSDictionary dictionaryWithObject:#"Type a LOT" forKey:#"exerciseName"];
exercises = [exercises arrayByAddingObject:newExercise];
NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithDictionary:[list objectAtIndex:10]];
[dict setObject:exercises forKey:#"exercises"];
[list replaceObjectAtIndex:10 withObject:dict];
[changeMe setObject:list forKey:#"list"];
Once you make your change, it is important to remember to write changeMe to the plist file in the documents directory.
The easiest way to read and write property lists is to use the NSArray or NSDictionary classes. Your screenshot appears to be an array at the top level, so I will use that assumption for my examples.
First you need paths to the user file and original file.
NSString *pathToUserFile; // Get a path to the file in the documents directory
NSString *pathToDefaultFile; // Get a path to the original file in the application bundle
You then attempt to load the
NSMutableArray *userData;
NSArray *temporary = [NSArray arrayWithContentsOfFile:pathToUserFile];
if(!temporary) temporary = [NSArray arrayWithContentsOfFile:pathToDefaultFile];
Since it appears that you are using multiple layers containers, I am assuming that you will need the innermost arrays and dictionaries to be mutable. The normal initialization of NSMutableArray will not do this, so you need to use CFPropertyListCreateDeepCopy with the options set to have mutable containers.
userData = (NSMutableArray *)CFPropertyListCreateDeepCopy(NULL,(CFArrayRef)temporary,kCFPropertyListMutableContainers);
You now have a mutable object representing your data. You can add objects or modify existing objects the same way you handle any array, but you can only add strings, numbers, data objects, dictionaries, arrays, and dates, since those are the only types valid in property lists.
[userData addObject:newDataObject];
Finally, you write the data out to the file in the documents directory. The writeToFile:atomically: method will attempt to write out a property list, and return YES if successful. It will fail and return NO if the file could not be written, or if the contents are not all valid property list objects.
if(![userData writeToFile:pathToUserFile atomically:YES]) {
NSLog(#"Error writing to file");
}
i have a array with data,i want to load the dictiionary type element with array......
NSArray *thisArray = [[NSArray alloc] initWithContentsOfFile:path];
NSSdictionary *state;
how to load the content with array....
any help appreciated...
The usual pitfall is getting the path right for the file, other than that it is pretty straight forward... if I understand the question correctly
NSString* path = [[NSBundle mainBundle]pathForResource:#"someDataFile" ofType:#"plist"];
NSDictionary *state = [[NSDictionary alloc]initWithContentsOfFile:path];