- •C and Objective-C
- •How this book works
- •How the life of a programmer works
- •Installing Apple’s developer tools
- •Getting started with Xcode
- •Where do I start writing code?
- •How do I run my program?
- •So what is a program?
- •Don’t stop
- •Types
- •A program with variables
- •Challenge
- •Boolean variables
- •When should I use a function?
- •How do I write and use a function?
- •How functions work together
- •Local variables, frames, and the stack
- •Recursion
- •Looking at the frames in the debugger
- •return
- •Global and static variables
- •Challenge
- •printf()
- •Integer operations
- •Integer division
- •Operator shorthand
- •Floating-point numbers
- •Tokens for displaying floating-point numbers
- •The while loop
- •The for loop
- •break
- •continue
- •The do-while loop
- •Challenge
- •Getting addresses
- •Storing addresses in pointers
- •Getting the data at an address
- •How many bytes?
- •NULL
- •Stylish pointer declarations
- •Challenges
- •Writing pass-by-reference functions
- •Avoid dereferencing NULL
- •Creating and using your first object
- •Message anatomy
- •Objects in memory
- •Challenge
- •Nesting message sends
- •Multiple arguments
- •Sending messages to nil
- •Challenge
- •Challenge
- •NSMutableArray
- •Reference pages
- •Quick Help
- •Other options and resources
- •Accessor methods
- •Dot notation
- •Properties
- •self
- •Multiple files
- •Challenge
- •Overriding methods
- •super
- •Challenge
- •Object ownership and ARC
- •Creating the Asset class
- •Adding a to-many relationship to Employee
- •Challenge
- •Retain cycles
- •Weak references
- •Zeroing of weak references
- •For the More Curious: Manual reference counting and ARC History
- •Retain count rules
- •NSArray/NSMutableArray
- •Immutable objects
- •Sorting
- •Filtering
- •NSSet/NSMutableSet
- •NSDictionary/NSMutableDictionary
- •Preprocessor directives
- •#include and #import
- •#define
- •Global variables
- •enum
- •#define vs global variables
- •Writing an NSString to a file
- •Reading files with NSString
- •Writing an NSData object to a file
- •Reading an NSData from a file
- •Target-action
- •Helper objects
- •Notifications
- •Which to use?
- •Callbacks and object ownership
- •Challenge
- •Getting started with iTahDoodle
- •BNRAppDelegate
- •Adding a C helper function
- •Objects in iTahDoodle
- •Model-View-Controller
- •The application delegate
- •Setting up views
- •Running on the iOS simulator
- •Wiring up the table view
- •Adding new tasks
- •Saving task data
- •For the More Curious: What about main()?
- •Edit BNRDocument.h
- •A look at Interface Builder
- •Edit BNRDocument.xib
- •Making connections
- •Revisiting MVC
- •Edit BNRDocument.m
- •Writing init methods
- •A basic init method
- •Using accessors
- •init methods that take arguments
- •Deadly init methods
- •Property attributes
- •Mutability
- •Lifetime specifiers
- •copy
- •More about copying
- •Advice on atomic vs. nonatomic
- •Key-value coding
- •Non-object types
- •Defining blocks
- •Using blocks
- •Declaring a block variable
- •Assigning a block
- •Passing in a block
- •typedef
- •Return values
- •Memory management
- •The block-based future
- •Challenges
- •Anonymous block
- •NSNotificationCenter
- •Bitwise-OR
- •Bitwise-AND
- •Other bitwise operators
- •Exclusive OR
- •Complement
- •Left-shift
- •Right-shift
- •Using enum to define bit masks
- •More bytes
- •Challenge
- •char
- •char *
- •String literals
- •Converting to and from NSString
- •Next Steps
- •Index
Chapter 28 Your First Cocoa Application
BNRDocument instance, which is why we wired its action. The BNRDocument, however, does not need to send messages to the button, and so it does not need a pointer to it.
Revisiting MVC
Now that you’ve laid out your user interface, let’s have a look at the object diagram for this project:
Figure 28.15 Object diagram for TahDoodle
NSDocument, the superclass from which your BNRDocument class inherits, is an interesting beast. At first glance, it appears to be a model object. If you look up the class reference for NSDocument, though, you’ll discover that it’s more of a controller than anything else. NSDocument coordinates various disk-related activities and connects directly to the views responsible for issuing user input. When
you create the NSDocument subclass BNRDocument, you added pointers to the real model objects (an
NSMutableArray of NSString objects).
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:.
#import "BNRDocument.h"
@implementation BNRDocument
#pragma mark - NSDocument Overrides
- (NSString *)windowNibName
{
return @"BNRDocument";
}
202
Edit BNRDocument.m
#pragma mark - Actions
- (IBAction)createNewItem:(id)sender
{
// If there's no array yet, go ahead and create one to store our new task if (!todoItems) {
todoItems = [NSMutableArray array];
}
[todoItems addObject:@"New Item"];
//-reloadData tells the table view to refresh and ask its dataSource
//(which happens to be this BNRDocument object in this case)
//for new data to display
[itemTableView reloadData];
//-updateChangeCount: tells the application whether or not the document
//has unsaved changes. NSChangeDone flags the document as unsaved. [self updateChangeCount:NSChangeDone];
}
Now implement the required table view data source methods (as defined by the
NSTableViewDataSource protocol):
#pragma mark Data Source Methods
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tv
{
//This table view is meant to display the todoItems,
//so the number of entries in the table view will be the same
//as the number of objects in the array.
return [todoItems count];
}
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row
{
//Return the item from todoItems that corresponds to the cell
//that the table view wants to display
return [todoItems objectAtIndex:row];
}
- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row
{
//When the user changes a to-do item on the table view,
//update the todoItems array
[todoItems replaceObjectAtIndex:row withObject:object]; // And then flag the document as having unsaved changes. [self updateChangeCount:NSChangeDone];
}
Build and run the program. TahDoodle will appear on the screen, and you can add and change todo 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:
203
)/"" ( + %
2A "% N ;? 2AA "% ? 2
2AV ""% V
&
. # # ! # ! #
E I # # N6' ; ! #
# /
( "1 ( ! " "
@ # 7
# ) RN6%! L " "S
8
/ ! # " N6' ;
N6' # ) RN6 "-6 D
# E "-U #
UN6 "-F%-? G *G&
U&
U ! 5 S
! ! " / # N6' ;
! #
.
BNND% @ 2A "%
;? 2AA "% ? 2
2AV ""% V
&
. # # # ! # #
E # # N6' ; # I # ! ! # !
5I ! #
# ) RN6 "-6 D
"-E ' U#
UN6 "-%! 2
UNH--
U ! 5 S
! ! ! # # !
! # @)
.
& & ' )G ' ' ' * N65! + ' *N65
"-E ' U U U U) ) ) * + N65+ ) & & ) !
) ) ! + ) " * !
&& '
*- ) !
C * )N65 & " )' +N65 * + " & ' & !
)4!
Part V
Advanced Objective-C
You now know enough Objective-C to get started with iOS or Cocoa programming. But don’t rush off just yet. These next chapters provide a gentle discussion of techniques and concepts that will be useful in your first year as an Objective-C programmer.
This page intentionally left blank