We can tell from the instance variable declarations that the iTahDoodle application will include at least four additional objects. However, before we get to these objects, you’re going to write a C function. In Objective-C, we usually get things done with methods rather than functions. So when we do use a C function in an Objective-C application, we often refer to them as “helper” functions.
iTahDoodle will store the user’s tasks as a property list – an XML file. Therefore, you will need a way to get this file’s location while your application is running. You’re going to write a C function that returns that file path as an NSString.
To add a helper function to your application, you first need to declare it in BNRAppDelegate.h.
#import <UIKit/UIKit.h> // Declare a helper function that we will use to get a path // to the location on disk where we can save the to-do list NSString *docPath(void); @interface BNRAppDelegate : UIResponder <UIApplicationDelegate> { UITableView *taskTable; UITextField *taskField; UIButton *insertButton; NSMutableArray *tasks; } - (void)addTask:(id)sender; @property (strong, nonatomic) UIWindow *window; @end
Notice that you declare docPath() above the class declaration. That’s because even though docPath() is declared in the file BNRAppDelegate.h, it is not part of the BNRAppDelegate class. In fact, this function could have its own pair of files in the iTahDoodle project. However, because there is just one of these helper functions in iTahDoodle, we’re putting it the app delegate’s class files to keep things simple.
Now open BNRAppDelegate.m and implement your helper function. Again, because docPath() is not part of the class, implement it after the #import but before the @implementation line (which is where the implementation of the class begins).
#import "BNRAppDelegate.h" // Helper function to fetch the path to our to-do data stored on disk NSString *docPath() { NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); return [[pathList objectAtIndex:0] stringByAppendingPathComponent:@"data.td"]; } @implementation
The docPath() function calls another C function, NSSearchPathForDirectoriesInDomains(). This function searches for directories that match specific criteria and returns an array of them. Don’t worry about what the arguments are; in nearly all iOS applications you will ever write, you’ll pass in the exact same three arguments and get back an array with exactly one item. (If you’re curious about how it works, you’ll find NSSearchPathForDirectoriesInDomains() in the Foundation Functions Reference in the developer documentation.)