Graphics Programs Reference
In-Depth Information
Open BNRItem.h in the editor area by clicking on it in the project navigator. The file
currently looks like this:
#import <Foundation/Foundation.h>
@interface BNRItem : NSObject
@end
Objective-C retains all the keywords of the C language, and additional keywords specific
to Objective-C are distinguishable by the @ prefix. To declare a class in Objective-C, you
use the keyword @interface followed by the name of this new class. After a colon
comes the name of the superclass. Objective-C only allows single inheritance, so you will
only ever see the following pattern:
@interface ClassName : SuperclassName
The @end directive indicates that the class has been fully declared.
Instance variables
So far, the BNRItem class doesn't add anything to its superclass NSObject . It needs
some item-like instance variables. An item, in our world, is going to have a name, a serial
number, a value, and a date of creation.
Instance variables for a class are declared in between curly brackets immediately after the
class declaration. In BNRItem.h , add four instance variables (and the curly brackets that
contain them) to the BNRItem class:
#import <Foundation/Foundation.h>
@interface BNRItem : NSObject
{
NSString *itemName;
NSString *serialNumber;
int valueInDollars;
NSDate *dateCreated;
}
@end
Now every instance of BNRItem has one spot for a simple integer and three spots to hold
references to objects, specifically two NSString instances and one NSDate instance.
(A reference is another word for pointer; the * denotes that the variable is a pointer.) Fig-
ure 2.12 shows an example of a BNRItem instance after its instance variables have been
given values.
Search WWH ::




Custom Search