@@ -19,20 +19,10 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
1919 // Create the window object to take up the entire bounds of the screen.
2020 self.window = [[UIWindow alloc ] initWithFrame: [[UIScreen mainScreen ] bounds ]];
2121
22- // Bring in the JSON from the file.
23- NSString *filePath = [[NSBundle mainBundle ] pathForResource: @" sports" ofType: @" json" ];
24- NSData *jsonData = [NSData dataWithContentsOfFile: filePath];
25-
26- // We can cast this to an array since we know for sure this is an array at the top level -
27- // NSJSONSerialization normally returns an id and you have to check whether it's an array
28- // or a dictionary.
29- NSError *error = nil ;
30- NSArray *sportsDictionaryArray = [NSJSONSerialization JSONObjectWithData: jsonData options: kNilOptions error: &error];
22+ NSArray *sportsDictionaryArray = [self arrayFromJSONFile ];
3123
32- if (error != nil ) {
33- // something failed in reading the json, log the error so we can figure out what went wrong.
34- NSLog (@" JSON error: %@ " , error);
35- }
24+ // Uncomment this and comment previous line out to see demonstration of loading same data from a plist.
25+ // NSArray *sportsDictionaryArray = [self arrayFromPlistFile];
3626
3727 // Create a mutable array to go through the list of dictionaries we're getting back
3828 NSMutableArray *sportsMutableArray = [NSMutableArray array ];
@@ -81,4 +71,77 @@ - (void)applicationWillTerminate:(UIApplication *)application
8171 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
8272}
8373
74+ #pragma mark - Data loaders
75+ -(NSArray *)arrayFromPlistFile
76+ {
77+ // Bring in the JSON from the file.
78+ NSString *filePath = [[NSBundle mainBundle ] pathForResource: @" sports" ofType: @" plist" ];
79+
80+ // Plists can be returned simply as an array with contents of file (note: if you know your
81+ // lowest-level object is a dictionary, you should set this up to return an NSDictionary and
82+ // use [NSDictionary dictionaryWithContentsOfFile:filePath] instead.
83+ return [NSArray arrayWithContentsOfFile: filePath];
84+ }
85+
86+ -(NSArray *)arrayFromJSONFile
87+ {
88+ // Bring in the JSON from the file.
89+ NSString *filePath = [[NSBundle mainBundle ] pathForResource: @" sports" ofType: @" json" ];
90+ NSData *fileData = [NSData dataWithContentsOfFile: filePath];
91+
92+ // We can cast this to an array since we know for sure this is an array at the top level -
93+ // NSJSONSerialization normally returns an id and you have to check whether it's an array
94+ // or a dictionary.
95+ NSError *error = nil ;
96+ NSArray *sportsDictionaryArray = [NSJSONSerialization JSONObjectWithData: fileData options: kNilOptions error: &error];
97+
98+ if (error != nil ) {
99+ // something failed in reading the json, log the error so we can figure out what went wrong.
100+ NSLog (@" JSON error: %@ " , error);
101+ }
102+
103+ return sportsDictionaryArray;
104+ }
105+
106+ #pragma mark - Data converters
107+ -(NSString *)documentsDirectory
108+ {
109+ NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
110+ NSString *documentsDirectory = [paths objectAtIndex: 0 ];
111+ return documentsDirectory;
112+ }
113+
114+ -(void )writeObjectToPlistFile : (id )dictionaryOrArrayToWrite
115+ {
116+ // Get the path to write the file to
117+ NSString *plistPath = [[self documentsDirectory ] stringByAppendingPathComponent: @" fromDictionary.plist" ];
118+
119+ // Log to be able to find the simulator file.
120+ NSLog (@" Plist path = %@ " , plistPath);
121+
122+ // Write directly to file.
123+ [dictionaryOrArrayToWrite writeToFile: plistPath atomically: YES ];
124+ }
125+
126+ -(void )writeDictionaryToJSONFile : (id )dictionaryOrArrayToWrite
127+ {
128+ NSError *error = nil ;
129+
130+ // Serialize the array or dictionary to a JSON object.
131+ NSData *jsonData = [NSJSONSerialization dataWithJSONObject: dictionaryOrArrayToWrite options: NSJSONWritingPrettyPrinted error: &error];
132+
133+ if (!error) {
134+ // Get the path to write the file to
135+ NSString *jsonPath = [[self documentsDirectory ] stringByAppendingPathComponent: @" fromDictionary.json" ];
136+
137+ // Log to be able to find the simulator file
138+ NSLog (@" JSON path = %@ " , jsonPath);
139+
140+ // Write serialized data to file.
141+ [jsonData writeToFile: jsonPath atomically: YES ];
142+ } else {
143+ NSLog (@" Error serializing JSON: %@ " , error.localizedDescription );
144+ }
145+ }
146+
84147@end
0 commit comments