I save an array of user objects in a plist list file as follows:
+(void) arrayToPlist: (NSArray*) plant_types{ NSMutableArray* dictionary_array = [NSMutableArray arrayWithCapacity: plant_types.count]; for(int i = 0; i<plant_types.count; i++){ PlantType* pt = [plant_types objectAtIndex: i]; [dictionary_array addObject: [pt toDict]]; } NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString* filePath = [rootPath stringByAppendingPathComponent:@"PlantTypes.plist"]; NSLog(@"Writing plist to: %@", filePath); [dictionary_array writeToFile: filePath atomically: YES]; }
Creates a file that looks like this:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <array> <dict> <key>name</key> <string>Autumn Olive</string> <key>radius</key> <real>10</real> </dict> <dict> <key>name</key> <string>Dwarf Cherry</string> <key>radius</key> <real>5</real> </dict> <dict> <key>name</key> <string>Bamboo</string> <key>radius</key> <real>2</real> </dict> <dict> <key>name</key> <string>Pomegranate</string> <key>radius</key> <real>6</real> </dict> <dict> <key>name</key> <string>Lupin</string> <key>radius</key> <real>0.60000002384185791</real> </dict> </array> </plist>
And download them as follows:
+(NSMutableArray*) arrayFromPlist{ NSLog(@"Loading array of plant types from plist."); NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString* filePath = [rootPath stringByAppendingPathComponent:@"PlantTypes.plist"]; NSFileManager* fileManager = [NSFileManager defaultManager]; if([fileManager fileExistsAtPath:filePath]){ NSLog(@"Plist file exists at expected location."); NSMutableArray* plant_dicts = [NSMutableArray arrayWithContentsOfFile:filePath]; NSLog(@"Loaded array with contents of file, got %d plant types.", plant_dicts.count); NSMutableArray* plant_types = [NSMutableArray arrayWithCapacity: plant_dicts.count]; for(int i = 0; i< plant_dicts.count; i++){ NSMutableDictionary* dict = [plant_dicts objectAtIndex: i]; [plant_types addObject: [PlantType fromDict: dict]]; } return plant_types; } NSLog(@"Plant Type plist file not found."); return NULL; }
Nothings crashing, but every time I return an NSMutableArray
zero size (and this log statement confirms that plant_dicts
is 0).
What am I doing wrong? All such problems that I see with arrayWithContentsOfFile:
are people creating plist manually or using an editor ... I think that creating this code from the code itself will not have these problems ...
Edit: I confirmed that I no longer get a result with a zero size (which is strange, there have been no changes in this section since publication). However, I am still not loading things correctly. Here are my fromDict and toDict methods.
The problem is that the “shape” line does not seem to load correctly. At least when I ask if there is a shape == @ "Circle", I understand that this is not so, even if it is clear in the NSLog instruction. I am doing the same comparison elsewhere in the code (when checking how to render the object), and it works fine there (so I assume there aren't any weird == vs .equals, like in Java).
Double editing: Wait, no [shape isEqualToString: @ "Circle"] .... why I didn’t have to use it before confusing me, but adding here helps.