Permalink
Browse files

- Added ability to read from Plist and actual Plist itself

- Added ability to write to Plist or JSON file
- Fixed name of the NBA.
  • Loading branch information...
1 parent 4cf7d63 commit 640c60caa73a75fa1d4c970252f4754574b5f252 @designatednerd committed Sep 19, 2013
Showing with 496 additions and 19 deletions.
  1. +76 −13 MLBJsonParser/AppDelegate.m
  2. +1 −1 MLBJsonParser/sports.json
  3. +410 −0 MLBJsonParser/sports.plist
  4. +9 −5 SportsJSONParser.xcodeproj/project.pbxproj
@@ -19,20 +19,10 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
//Create the window object to take up the entire bounds of the screen.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- //Bring in the JSON from the file.
- NSString *filePath = [[NSBundle mainBundle] pathForResource:@"sports" ofType:@"json"];
- NSData *jsonData = [NSData dataWithContentsOfFile:filePath];
-
- //We can cast this to an array since we know for sure this is an array at the top level -
- //NSJSONSerialization normally returns an id and you have to check whether it's an array
- //or a dictionary.
- NSError *error = nil;
- NSArray *sportsDictionaryArray = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
+ NSArray *sportsDictionaryArray = [self arrayFromJSONFile];
- if (error != nil) {
- //something failed in reading the json, log the error so we can figure out what went wrong.
- NSLog(@"JSON error: %@", error);
- }
+ //Uncomment this and comment previous line out to see demonstration of loading same data from a plist.
+// NSArray *sportsDictionaryArray = [self arrayFromPlistFile];
//Create a mutable array to go through the list of dictionaries we're getting back
NSMutableArray *sportsMutableArray = [NSMutableArray array];
@@ -81,4 +71,77 @@ - (void)applicationWillTerminate:(UIApplication *)application
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
+#pragma mark - Data loaders
+-(NSArray *)arrayFromPlistFile
+{
+ //Bring in the JSON from the file.
+ NSString *filePath = [[NSBundle mainBundle] pathForResource:@"sports" ofType:@"plist"];
+
+ //Plists can be returned simply as an array with contents of file (note: if you know your
+ //lowest-level object is a dictionary, you should set this up to return an NSDictionary and
+ //use [NSDictionary dictionaryWithContentsOfFile:filePath] instead.
+ return [NSArray arrayWithContentsOfFile:filePath];
+}
+
+-(NSArray *)arrayFromJSONFile
+{
+ //Bring in the JSON from the file.
+ NSString *filePath = [[NSBundle mainBundle] pathForResource:@"sports" ofType:@"json"];
+ NSData *fileData = [NSData dataWithContentsOfFile:filePath];
+
+ //We can cast this to an array since we know for sure this is an array at the top level -
+ //NSJSONSerialization normally returns an id and you have to check whether it's an array
+ //or a dictionary.
+ NSError *error = nil;
+ NSArray *sportsDictionaryArray = [NSJSONSerialization JSONObjectWithData:fileData options:kNilOptions error:&error];
+
+ if (error != nil) {
+ //something failed in reading the json, log the error so we can figure out what went wrong.
+ NSLog(@"JSON error: %@", error);
+ }
+
+ return sportsDictionaryArray;
+}
+
+#pragma mark - Data converters
+-(NSString *)documentsDirectory
+{
+ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
+ NSString *documentsDirectory = [paths objectAtIndex:0];
+ return documentsDirectory;
+}
+
+-(void)writeObjectToPlistFile:(id)dictionaryOrArrayToWrite
+{
+ //Get the path to write the file to
+ NSString *plistPath = [[self documentsDirectory] stringByAppendingPathComponent:@"fromDictionary.plist"];
+
+ //Log to be able to find the simulator file.
+ NSLog(@"Plist path = %@", plistPath);
+
+ //Write directly to file.
+ [dictionaryOrArrayToWrite writeToFile:plistPath atomically: YES];
+}
+
+-(void)writeDictionaryToJSONFile:(id)dictionaryOrArrayToWrite
+{
+ NSError *error = nil;
+
+ //Serialize the array or dictionary to a JSON object.
+ NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionaryOrArrayToWrite options:NSJSONWritingPrettyPrinted error:&error];
+
+ if (!error) {
+ //Get the path to write the file to
+ NSString *jsonPath = [[self documentsDirectory] stringByAppendingPathComponent:@"fromDictionary.json"];
+
+ //Log to be able to find the simulator file
+ NSLog(@"JSON path = %@", jsonPath);
+
+ //Write serialized data to file.
+ [jsonData writeToFile:jsonPath atomically:YES];
+ } else {
+ NSLog(@"Error serializing JSON: %@", error.localizedDescription);
+ }
+}
+
@end
@@ -134,7 +134,7 @@
},
{
"game": "Basketball",
- "organization_name": "National Basketball League",
+ "organization_name": "National Basketball Association",
"founded": "1946",
"leagues": [
{
Oops, something went wrong.

0 comments on commit 640c60c

Please sign in to comment.