Last week you learned what a property list is and got an overview of the PropertyList application you will create in the series. You also downloaded the the Xcode project you’ll develop in this Property List series.
Add Code in The AppDelegate Files
Today, you’ll add code in the AppDelegate class files to set up the application’s data model, so launch the PropertyListDemo project in Xcode.
Click the AppDelegate.h file to load it in the code editor window, then add code shown below in the file.
#import <UIKit/UIKit.h> @class MasterViewController; @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) MasterViewController *viewController; @property (nonatomic, retain) NSString *dataFilePath; @property (strong, nonatomic) NSMutableDictionary *fileContent; - (NSString *)getPathToDocumentsDir; - (void)addGlossary:(NSString *)word andField2:(NSString *)definition; - (NSMutableDictionary *)retrieveGlossary; @end
Switch to the AppDelegate.m file and add this code in the file.
#import "AppDelegate.h" #import "MasterViewController.h" @implementation AppDelegate - (NSString *)getPathToDocumentsDir { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDir = [paths objectAtIndex:0]; // Return the Documents directory return documentsDir; } - (void)addGlossary:(NSString *)word andField2:(NSString *)definition { // Get the full path to the plist file self.dataFilePath = [self.getPathToDocumentsDir stringByAppendingPathComponent:@"glossary.plist"]; // Fetch all content from the plist file NSMutableDictionary *allWords = [[NSMutableDictionary alloc] init]; allWords = [self retrieveGlossary]; // Add an entry in the allRecords variable [allWords setObject:definition forKey:word]; // Add all records back in the plist file [allWords writeToFile:self.dataFilePath atomically:YES]; } - (NSMutableDictionary *)retrieveGlossary { // Get the full path to the plist file self.dataFilePath = [self.getPathToDocumentsDir stringByAppendingPathComponent:@"glossary.plist"]; // Get all data from the plist file, assign it to the mutuable variable NSMutableDictionary *fileContent = [[NSMutableDictionary alloc] initWithContentsOfFile:self.dataFilePath]; // Return plist file's data or an empty array return fileContent; }
Next, add highlighted code in the didFinishLaunchingWithOptions method.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Get the full path to the plist file self.dataFilePath = [self.getPathToDocumentsDir stringByAppendingPathComponent:@"glossary.plist"]; if (![[NSFileManager defaultManager] fileExistsAtPath:self.dataFilePath]) { // The plist file does not exist, create and initialize it in the app's Documents folder NSDictionary *dicEntry = [[NSDictionary alloc] init]; [dicEntry writeToFile:self.dataFilePath atomically:YES]; } self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.viewController = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; return YES; }
What You’ve Done?
First of all you added code in the AppDelegate.h file to declared two property variables: dataFilePath and fileContent. The first one will hold the full path to the plist file that’s created in the app’s Documents folder. The second one will hold the content fetch from the plist file. Next, you declared these three methods in the AppDelegate.h file.
getPathToDocumentsDir – This method takes no arguments; however, it fetch and returns the full path to the Documents directory, which exists in the application’s sandbox. This Finder image shows the Documents folder in the PropertyListDemo app’s sandbox and the path the method returns.
addGlossary – This method takes a single argument definition which is a dictionary object, add it in the plist file. It returns nothing.
retrieveGlossary – This method fetch all content from the plist file and return it as an mutable dictionary object, to the caller of this method. If the plist file is empty, the method will return an empty mutable dictionary object instead.
In the AppDelegate.m file you entered code in methods you declared in the AppDelegate.h file. Finally, you added code in the didFinishLaunchingWithOptions method to create the plist file in the sandbox’s Documents folder, only if it doesn’t already exists there, then initialize it with an empty dictionary object called dicEntry.
Run The Application
Before wrapping up, run the application to test code you entered in the AppDelegate files. In a few seconds, Xcode loads the MasterViewController.xib file’s user interface in the iPhone Simulator as shown in the far left image below.
Use Finder to access the application’s sandbox. Once you’re there, expand the Documents folder and you should see the plist file listed there-see the top-right image.
Next, double-click the plist file to open it in Xcode’s Property List editor. As you can see in the bottom-right image, it contain only one item, which is an empty dictionary called Root.
That’s all for this week’s workshop. Next week, you will add code in the MasterViewController interface and implementation file. Until then, happy coding! 🙂