Edit BNRDocument.m

Now that you’ve got the user interface of your application created, configured, and connected, it’s time to get back to writing code. Click on BNRDocument.m in the project navigator to reopen it in the editor, and implement createNewItem:.

#​i​m​p​o​r​t​ ​"​B​N​R​D​o​c​u​m​e​n​t​.​h​"​

@​i​m​p​l​e​m​e​n​t​a​t​i​o​n​ ​B​N​R​D​o​c​u​m​e​n​t​

#​p​r​a​g​m​a​ ​m​a​r​k​ ​-​ ​N​S​D​o​c​u​m​e​n​t​ ​O​v​e​r​r​i​d​e​s​

-​ ​(​N​S​S​t​r​i​n​g​ ​*​)​w​i​n​d​o​w​N​i​b​N​a​m​e​
{​
 ​ ​ ​ ​r​e​t​u​r​n​ ​@​"​B​N​R​D​o​c​u​m​e​n​t​"​;​
}​

#​p​r​a​g​m​a​ ​m​a​r​k​ ​-​ ​A​c​t​i​o​n​s​

-​ ​(​I​B​A​c​t​i​o​n​)​c​r​e​a​t​e​N​e​w​I​t​e​m​:​(​i​d​)​s​e​n​d​e​r​
{​
 ​ ​ ​ ​/​/​ ​I​f​ ​t​h​e​r​e​'​s​ ​n​o​ ​a​r​r​a​y​ ​y​e​t​,​ ​g​o​ ​a​h​e​a​d​ ​a​n​d​ ​c​r​e​a​t​e​ ​o​n​e​ ​t​o​ ​s​t​o​r​e​ ​o​u​r​ ​n​e​w​ ​t​a​s​k​
 ​ ​ ​ ​i​f​ ​(​!​t​o​d​o​I​t​e​m​s​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​t​o​d​o​I​t​e​m​s​ ​=​ ​[​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​a​r​r​a​y​]​;​
 ​ ​ ​ ​}​

 ​ ​ ​ ​[​t​o​d​o​I​t​e​m​s​ ​a​d​d​O​b​j​e​c​t​:​@​"​N​e​w​ ​I​t​e​m​"​]​;​

 ​ ​ ​ ​/​/​ ​-​r​e​l​o​a​d​D​a​t​a​ ​t​e​l​l​s​ ​t​h​e​ ​t​a​b​l​e​ ​v​i​e​w​ ​t​o​ ​r​e​f​r​e​s​h​ ​a​n​d​ ​a​s​k​ ​i​t​s​ ​d​a​t​a​S​o​u​r​c​e​
 ​ ​ ​ ​/​/​ ​(​w​h​i​c​h​ ​h​a​p​p​e​n​s​ ​t​o​ ​b​e​ ​t​h​i​s​ ​B​N​R​D​o​c​u​m​e​n​t​ ​o​b​j​e​c​t​ ​i​n​ ​t​h​i​s​ ​c​a​s​e​)​
 ​ ​ ​ ​/​/​ ​f​o​r​ ​n​e​w​ ​d​a​t​a​ ​t​o​ ​d​i​s​p​l​a​y​
 ​ ​ ​ ​[​i​t​e​m​T​a​b​l​e​V​i​e​w​ ​r​e​l​o​a​d​D​a​t​a​]​;​

 ​ ​ ​ ​/​/​ ​-​u​p​d​a​t​e​C​h​a​n​g​e​C​o​u​n​t​:​ ​t​e​l​l​s​ ​t​h​e​ ​a​p​p​l​i​c​a​t​i​o​n​ ​w​h​e​t​h​e​r​ ​o​r​ ​n​o​t​ ​t​h​e​ ​d​o​c​u​m​e​n​t​
 ​ ​ ​ ​/​/​ ​h​a​s​ ​u​n​s​a​v​e​d​ ​c​h​a​n​g​e​s​.​ ​N​S​C​h​a​n​g​e​D​o​n​e​ ​f​l​a​g​s​ ​t​h​e​ ​d​o​c​u​m​e​n​t​ ​a​s​ ​u​n​s​a​v​e​d​.​
 ​ ​ ​ ​[​s​e​l​f​ ​u​p​d​a​t​e​C​h​a​n​g​e​C​o​u​n​t​:​N​S​C​h​a​n​g​e​D​o​n​e​]​;​
}​

Now implement the required table view data source methods (as defined by the NSTableViewDataSource protocol):

#​p​r​a​g​m​a​ ​m​a​r​k​ ​D​a​t​a​ ​S​o​u​r​c​e​ ​M​e​t​h​o​d​s​

-​ ​(​N​S​I​n​t​e​g​e​r​)​n​u​m​b​e​r​O​f​R​o​w​s​I​n​T​a​b​l​e​V​i​e​w​:​(​N​S​T​a​b​l​e​V​i​e​w​ ​*​)​t​v​
{​
 ​ ​ ​ ​/​/​ ​T​h​i​s​ ​t​a​b​l​e​ ​v​i​e​w​ ​i​s​ ​m​e​a​n​t​ ​t​o​ ​d​i​s​p​l​a​y​ ​t​h​e​ ​t​o​d​o​I​t​e​m​s​,​
 ​ ​ ​ ​/​/​ ​s​o​ ​t​h​e​ ​n​u​m​b​e​r​ ​o​f​ ​e​n​t​r​i​e​s​ ​i​n​ ​t​h​e​ ​t​a​b​l​e​ ​v​i​e​w​ ​w​i​l​l​ ​b​e​ ​t​h​e​ ​s​a​m​e​
 ​ ​ ​ ​/​/​ ​a​s​ ​t​h​e​ ​n​u​m​b​e​r​ ​o​f​ ​o​b​j​e​c​t​s​ ​i​n​ ​t​h​e​ ​a​r​r​a​y​.​
 ​ ​ ​ ​r​e​t​u​r​n​ ​[​t​o​d​o​I​t​e​m​s​ ​c​o​u​n​t​]​;​
}​

-​ ​(​i​d​)​t​a​b​l​e​V​i​e​w​:​(​N​S​T​a​b​l​e​V​i​e​w​ ​*​)​t​a​b​l​e​V​i​e​w​
 ​ ​ ​ ​ ​ ​o​b​j​e​c​t​V​a​l​u​e​F​o​r​T​a​b​l​e​C​o​l​u​m​n​:​(​N​S​T​a​b​l​e​C​o​l​u​m​n​ ​*​)​t​a​b​l​e​C​o​l​u​m​n​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​r​o​w​:​(​N​S​I​n​t​e​g​e​r​)​r​o​w​
{​
 ​ ​ ​ ​/​/​ ​R​e​t​u​r​n​ ​t​h​e​ ​i​t​e​m​ ​f​r​o​m​ ​t​o​d​o​I​t​e​m​s​ ​t​h​a​t​ ​c​o​r​r​e​s​p​o​n​d​s​ ​t​o​ ​t​h​e​ ​c​e​l​l​
 ​ ​ ​ ​/​/​ ​t​h​a​t​ ​t​h​e​ ​t​a​b​l​e​ ​v​i​e​w​ ​w​a​n​t​s​ ​t​o​ ​d​i​s​p​l​a​y​
 ​ ​ ​ ​r​e​t​u​r​n​ ​[​t​o​d​o​I​t​e​m​s​ ​o​b​j​e​c​t​A​t​I​n​d​e​x​:​r​o​w​]​;​
}​

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

Build and run the program. TahDoodle will appear on the screen, and you can add and change to-do items. The big missing feature, however, is the ability to save and reopen a to-do list. To make this happen, you need to override the following 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​
 ​ ​ ​ ​/​/​ ​W​e​ ​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​'​s​ ​n​o​ ​a​r​r​a​y​,​ ​w​e​'​l​l​ ​w​r​i​t​e​ ​o​u​t​ ​a​n​ ​e​m​p​t​y​ ​a​r​r​a​y​ ​f​o​r​ ​n​o​w​
 ​ ​ ​ ​i​f​ ​(​!​t​o​d​o​I​t​e​m​s​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​t​o​d​o​I​t​e​m​s​ ​=​ ​[​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​a​r​r​a​y​]​;​
 ​ ​ ​ ​}​

 ​ ​ ​ ​/​/​ ​P​a​c​k​ ​o​u​r​ ​t​o​d​o​I​t​e​m​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​:​t​o​d​o​I​t​e​m​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​:​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​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​e​r​r​o​r​:​o​u​t​E​r​r​o​r​]​;​

 ​ ​ ​ ​/​/​ ​r​e​t​u​r​n​ ​o​u​r​ ​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​
 ​ ​ ​ ​/​/​ ​W​e​ ​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​ ​o​u​r​ ​t​o​d​o​I​t​e​m​s​
 ​ ​ ​ ​t​o​d​o​I​t​e​m​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​:​0​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​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​ ​(​t​o​d​o​I​t​e​m​s​ ​!​=​ ​n​i​l​)​;​
}​

Notice that for the first time, you’re implementing a method that takes in an NSError**. In this case, we 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. Now you can save and load task lists.

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

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