- •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 16 Developer Documentation
Figure 16.6 Quick Help pane
Note that when you select something else, the Quick Help pane will immediately update to show the documentation for the new selection.
Other options and resources
If you Option-double-click on any text, the Organizer will search for that string.
If you Command-click on any class, function, or method, Xcode will show you the file where it is declared. (Try Command-clicking the word “NSMutableArray”.)
Figure 16.7 The declaration of NSMutableArray
98
Other options and resources
If there are several declarations that match, you’ll get a pop-up when you Command-click. Try Command-clicking addObject:.
From time to time, Xcode will call back to the mothership to see if there is any updated documentation. It will download the updates and re-index them for easy searching. You can trigger an immediate check for updates in the preferences panel for Xcode.
Apple hosts a collection of discussion mailing lists at http://lists.apple.com/, and several of them are devoted to developer topics. If you can’t find an answer in the docs, try searching the archives of the lists. (As a courtesy to the thousands of people on each list, please search the archives carefully before posting a question.)
There is one more resource that you should know about: stackoverflow.com. This website is a very popular place where programmers ask and answer questions. If you can think up a few keywords that describe the sort of problem you are having, you can probably find an answer there.
99
This page intentionally left blank
17
Your First Class
So far, you have only used classes created by Apple. Now you get to write your own class. Remember that a class describes objects in two ways:
•methods (both instance methods and class methods) implemented by the class
•instance variables within each instance of the class
You’re going to write a Person class, similar to the struct Person you wrote in Chapter 10. Your class will be defined in two files: Person.h and Person.m. Person.h, known as the header or interface file, will contain the declarations of instance variables and methods. Person.m is known as the implementation file. This is where you write out the steps for, or implement, each method.
Create a new project: a Foundation Command Line Tool named BMITime.
To create a new class, select File → New → New File.... From the Mac OS X section on the left, select Cocoa and choose the Objective-C class template. Name your class Person and make it a subclass of NSObject. Finally, make sure the BMITime target is checked and click Save.
Notice that the new class files, Person.h and Person.m, now appear in the project navigator. Open Person.h. Declare two instance variables and three instance methods:
#import <Foundation/Foundation.h>
//The class Person inherits all the instance variables
//and methods defined by the class NSObject @interface Person : NSObject
{
// It has two instance variables float heightInMeters;
int weightInKilos;
}
//You will be able to set those instance variables using these methods
- (void)setHeightInMeters:(float)h; - (void)setWeightInKilos:(int)w;
//This method calculates the Body Mass Index
- (float)bodyMassIndex;
@end
Notice that you declared the instance variables inside of curly braces and you declared the methods after the variables and outside of the curly braces.
101
Chapter 17 Your First Class
To the compiler, this code says “I am defining a new class called Person that has all the methods and instance variables of the class NSObject. Furthermore, I’m adding in two new instance variables: a float called heightInMeters and an int called weightInKilos. Also, I’m going to add three instance methods, which will be implemented in Person.m.”
Notice that setHeightInMeters: expects a float argument and does not return a value, setWeightInKilos: expects an int argument and does not return a value, and bodyMassIndex takes no arguments and returns a float.
Now open Person.m. Delete any existing methods and implement the methods you declared:
#import "Person.h"
@implementation Person
-(void)setHeightInMeters:(float)h
{
heightInMeters = h;
}
-(void)setWeightInKilos:(int)w
{
weightInKilos = w;
}
- (float)bodyMassIndex
{
return weightInKilos / (heightInMeters * heightInMeters);
}
@end
Note that Xcode imported Person.h for you. Also note that the names of the methods you implement must exactly match the ones you declared in the header file. In Xcode, this is easy; as you start typing a method in the implementation file, Xcode will suggest names of methods you’ve already declared.
Now that you’ve implemented all the methods you declared in Person.h, your class is complete. Edit main.m to exercise it a bit:
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
// Create an instance of Person
Person *person = [[Person alloc] init];
//Give the instance variables interesting values [person setWeightInKilos:96];
[person setHeightInMeters:1.8];
//Call the bodyMassIndex method
float bmi = [person bodyMassIndex]; NSLog(@"person has a BMI of %f", bmi);
}
return 0;
}
102