Adding a C helper function

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.

#​i​m​p​o​r​t​ ​<​U​I​K​i​t​/​U​I​K​i​t​.​h​>​

/​/​ ​D​e​c​l​a​r​e​ ​a​ ​h​e​l​p​e​r​ ​f​u​n​c​t​i​o​n​ ​t​h​a​t​ ​w​e​ ​w​i​l​l​ ​u​s​e​ ​t​o​ ​g​e​t​ ​a​ ​p​a​t​h​
/​/​ ​t​o​ ​t​h​e​ ​l​o​c​a​t​i​o​n​ ​o​n​ ​d​i​s​k​ ​w​h​e​r​e​ ​w​e​ ​c​a​n​ ​s​a​v​e​ ​t​h​e​ ​t​o​-​d​o​ ​l​i​s​t​
N​S​S​t​r​i​n​g​ ​*​d​o​c​P​a​t​h​(​v​o​i​d​)​;​

@​i​n​t​e​r​f​a​c​e​ ​B​N​R​A​p​p​D​e​l​e​g​a​t​e​ ​:​ ​U​I​R​e​s​p​o​n​d​e​r​
<​U​I​A​p​p​l​i​c​a​t​i​o​n​D​e​l​e​g​a​t​e​>​
{​
 ​ ​ ​ ​U​I​T​a​b​l​e​V​i​e​w​ ​*​t​a​s​k​T​a​b​l​e​;​
 ​ ​ ​ ​U​I​T​e​x​t​F​i​e​l​d​ ​*​t​a​s​k​F​i​e​l​d​;​
 ​ ​ ​ ​U​I​B​u​t​t​o​n​ ​*​i​n​s​e​r​t​B​u​t​t​o​n​;​

 ​ ​ ​ ​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​*​t​a​s​k​s​;​
}​

-​ ​(​v​o​i​d​)​a​d​d​T​a​s​k​:​(​i​d​)​s​e​n​d​e​r​;​

@​p​r​o​p​e​r​t​y​ ​(​s​t​r​o​n​g​,​ ​n​o​n​a​t​o​m​i​c​)​ ​U​I​W​i​n​d​o​w​ ​*​w​i​n​d​o​w​;​

@​e​n​d​

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).

#​i​m​p​o​r​t​ ​"​B​N​R​A​p​p​D​e​l​e​g​a​t​e​.​h​"​


/​/​ ​H​e​l​p​e​r​ ​f​u​n​c​t​i​o​n​ ​t​o​ ​f​e​t​c​h​ ​t​h​e​ ​p​a​t​h​ ​t​o​ ​o​u​r​ ​t​o​-​d​o​ ​d​a​t​a​ ​s​t​o​r​e​d​ ​o​n​ ​d​i​s​k​
N​S​S​t​r​i​n​g​ ​*​d​o​c​P​a​t​h​(​)​
{​
 ​ ​ ​ ​N​S​A​r​r​a​y​ ​*​p​a​t​h​L​i​s​t​ ​=​ ​N​S​S​e​a​r​c​h​P​a​t​h​F​o​r​D​i​r​e​c​t​o​r​i​e​s​I​n​D​o​m​a​i​n​s​(​N​S​D​o​c​u​m​e​n​t​D​i​r​e​c​t​o​r​y​,​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​N​S​U​s​e​r​D​o​m​a​i​n​M​a​s​k​,​ ​Y​E​S​)​;​
 ​ ​ ​ ​r​e​t​u​r​n​ ​[​[​p​a​t​h​L​i​s​t​ ​o​b​j​e​c​t​A​t​I​n​d​e​x​:​0​]​ ​s​t​r​i​n​g​B​y​A​p​p​e​n​d​i​n​g​P​a​t​h​C​o​m​p​o​n​e​n​t​:​@​"​d​a​t​a​.​t​d​"​]​;​
}​


@​i​m​p​l​e​m​e​n​t​a​t​i​o​n​

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.)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset