Saving and loading data

To add the ability to save and reopen a task list, you need to override two methods inherited from BNRDocument’s superclass, NSDocument:

-​ ​(​N​S​D​a​t​a​ ​*​)​d​a​t​a​O​f​T​y​p​e​:​(​N​S​S​t​r​i​n​g​ ​*​)​t​y​p​e​N​a​m​e​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​e​r​r​o​r​:​(​N​S​E​r​r​o​r​ ​*​*​)​o​u​t​E​r​r​o​r​
{​
 ​ ​ ​ ​/​/​ ​T​h​i​s​ ​m​e​t​h​o​d​ ​i​s​ ​c​a​l​l​e​d​ ​w​h​e​n​ ​o​u​r​ ​d​o​c​u​m​e​n​t​ ​i​s​ ​b​e​i​n​g​ ​s​a​v​e​d​
 ​ ​ ​ ​/​/​ ​Y​o​u​ ​a​r​e​ ​e​x​p​e​c​t​e​d​ ​t​o​ ​h​a​n​d​ ​t​h​e​ ​c​a​l​l​e​r​ ​a​n​ ​N​S​D​a​t​a​ ​o​b​j​e​c​t​ ​w​r​a​p​p​i​n​g​ ​o​u​r​ ​d​a​t​a​
 ​ ​ ​ ​/​/​ ​s​o​ ​t​h​a​t​ ​i​t​ ​c​a​n​ ​b​e​ ​w​r​i​t​t​e​n​ ​t​o​ ​d​i​s​k​
 ​ ​ ​ ​/​/​ ​I​f​ ​t​h​e​r​e​ ​i​s​ ​n​o​ ​a​r​r​a​y​,​ ​w​r​i​t​e​ ​o​u​t​ ​a​n​ ​e​m​p​t​y​ ​a​r​r​a​y​
 ​ ​ ​ ​i​f​ ​(​!​s​e​l​f​.​t​a​s​k​s​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​s​e​l​f​.​t​a​s​k​s​ ​=​ ​[​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​a​r​r​a​y​]​;​
 ​ ​ ​ ​}​

 ​ ​ ​ ​/​/​ ​P​a​c​k​ ​t​h​e​ ​t​a​s​k​s​ ​a​r​r​a​y​ ​i​n​t​o​ ​a​n​ ​N​S​D​a​t​a​ ​o​b​j​e​c​t​
 ​ ​ ​ ​N​S​D​a​t​a​ ​*​d​a​t​a​ ​=​ ​[​N​S​P​r​o​p​e​r​t​y​L​i​s​t​S​e​r​i​a​l​i​z​a​t​i​o​n​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​d​a​t​a​W​i​t​h​P​r​o​p​e​r​t​y​L​i​s​t​:​s​e​l​f​.​t​a​s​k​s​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​f​o​r​m​a​t​:​N​S​P​r​o​p​e​r​t​y​L​i​s​t​X​M​L​F​o​r​m​a​t​_​v​1​_​0​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​o​p​t​i​o​n​s​:​0​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​e​r​r​o​r​:​o​u​t​E​r​r​o​r​]​;​

 ​ ​ ​ ​/​/​ ​R​e​t​u​r​n​ ​t​h​e​ ​n​e​w​l​y​-​p​a​c​k​e​d​ ​N​S​D​a​t​a​ ​o​b​j​e​c​t​
 ​ ​ ​ ​r​e​t​u​r​n​ ​d​a​t​a​;​
}​

-​ ​(​B​O​O​L​)​r​e​a​d​F​r​o​m​D​a​t​a​:​(​N​S​D​a​t​a​ ​*​)​d​a​t​a​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​o​f​T​y​p​e​:​(​N​S​S​t​r​i​n​g​ ​*​)​t​y​p​e​N​a​m​e​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​e​r​r​o​r​:​(​N​S​E​r​r​o​r​ ​*​*​)​o​u​t​E​r​r​o​r​
{​
 ​ ​ ​ ​/​/​ ​T​h​i​s​ ​m​e​t​h​o​d​ ​i​s​ ​c​a​l​l​e​d​ ​w​h​e​n​ ​a​ ​d​o​c​u​m​e​n​t​ ​i​s​ ​b​e​i​n​g​ ​l​o​a​d​e​d​
 ​ ​ ​ ​/​/​ ​Y​o​u​ ​a​r​e​ ​h​a​n​d​e​d​ ​a​n​ ​N​S​D​a​t​a​ ​o​b​j​e​c​t​ ​a​n​d​ ​e​x​p​e​c​t​e​d​ ​t​o​ ​p​u​l​l​ ​o​u​r​ ​d​a​t​a​ ​o​u​t​ ​o​f​ ​i​t​
 ​ ​ ​ ​/​/​ ​E​x​t​r​a​c​t​ ​t​h​e​ ​t​a​s​k​s​
 ​ ​ ​ ​s​e​l​f​.​t​a​s​k​s​ ​=​ ​[​N​S​P​r​o​p​e​r​t​y​L​i​s​t​S​e​r​i​a​l​i​z​a​t​i​o​n​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​p​r​o​p​e​r​t​y​L​i​s​t​W​i​t​h​D​a​t​a​:​d​a​t​a​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​o​p​t​i​o​n​s​:​N​S​P​r​o​p​e​r​t​y​L​i​s​t​M​u​t​a​b​l​e​C​o​n​t​a​i​n​e​r​s​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​f​o​r​m​a​t​:​N​U​L​L​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​e​r​r​o​r​:​o​u​t​E​r​r​o​r​]​;​

 ​ ​ ​ ​/​/​ ​r​e​t​u​r​n​ ​s​u​c​c​e​s​s​ ​o​r​ ​f​a​i​l​u​r​e​ ​d​e​p​e​n​d​i​n​g​ ​o​n​ ​s​u​c​c​e​s​s​ ​o​f​ ​t​h​e​ ​a​b​o​v​e​ ​c​a​l​l​
 ​ ​ ​ ​r​e​t​u​r​n​ ​(​s​e​l​f​.​t​a​s​k​s​ ​!​=​ ​n​i​l​)​;​
}​

Notice that you are implementing a method that takes in an NSError**. In this case, you are merely handing back the NSError generated by propertyListWithData:options:format:error:, but you could also create and hand back a new NSError as well, depending on the nature of the failure.

Build and run the application again. Add some tasks to the list. Save and close the list (using the familiar menu commands or keyboard shortcuts), and then reopen it. Congratulations! TahDoodle is complete.

So when should you use Interface Builder to create your user interface and when should you set up views programmatically? Under simple circumstances, either will work. iTahDoodle’s interface could have been built using Interface Builder; TahDoodle’s views could have been created programmatically.

In general, however, the more complex your user interface, the more sense it makes to use Interface Builder.

Now that you have seen more of Xcode, take a look at the tear-out card at the back of this book. This card contains keyboard shortcuts for navigating around Xcode. As you continue with Xcode, use this card to find shortcuts that will save you time and clicks.

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

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