iOS Programming: The Big Nerd Ranch Guide, 3/e (Big Nerd Ranch Guides)

iOS Programming: The Big Nerd Ranch Guide, 3/e (Big Nerd Ranch Guides) by Aaron Hillegass, Joe Conway Page A

Book: iOS Programming: The Big Nerd Ranch Guide, 3/e (Big Nerd Ranch Guides) by Aaron Hillegass, Joe Conway Read Free Book Online
Authors: Aaron Hillegass, Joe Conway
Tags: COM051370, Big Nerd Ranch Guides, iPhone / iPad Programming
Ads: Link
Figure 2.8 ). A class inherits the behavior of its superclass, which means, at a minimum, every class inherits the methods and instance variables defined in NSObject .
     
    As the top superclass, NSObject ’s role is to implement the basic behavior of every object in Cocoa Touch. Three of the methods NSObject implements are alloc , init , and description . (We sometimes say “ description is a method on NSObject ” and mean the same thing.)
     
    Figure 2.8  Class hierarchy
     
    A subclass adds methods and instance variables to extend the behavior of its superclass. For example, NSMutableArray extends NSArray ’s ability to hold pointers to objects by adding the ability to dynamically add and remove objects.
     
    A subclass can also override methods of its superclass. For example, sending the description message to an NSObject returns the object’s class and its address in memory, like this: .
     
    A subclass of NSObject can override this method to return something that better describes an instance of that subclass. For example, NSString overrides description to return the string itself. NSArray overrides description to return the description of every object in the array.
     
    Creating an NSObject subclass
    In this section, you’re going to create a subclass of NSObject named BNRItem . An instance of the BNRItem class will represent an item that a person owns in the real world. To create a new class in Xcode , choose File → New → New File... . In the lefthand table of the panel that appears, select Cocoa from the Mac OS X section. Then select Objective-C class from the upper panel and hit Next ( Figure 2.9 ).
     
    Figure 2.9  Creating a class
     
    On the next panel, name this new class BNRItem . Select NSObject as the superclass and click Next , as shown in Figure 2.10 .
     
    Figure 2.10  Choosing a superclass
     
    A panel will drop down that prompts you to create the files for this new class ( Figure 2.11 ). When creating a new class for a project, you want to save the files that describe it inside the project’s source directory on the filesystem. By default, the current project directory is already selected for you. You can also choose the group in the project navigator that these files will be added to. Because these groups are simply for organizing and because this project is very small, just stick with the default. Make sure the checkbox is selected for the RandomPossessions target. This ensures that this class will be compiled when the RandomPossessions project is built. Click Create .
     
    Figure 2.11  Saving class files
     
    Creating the BNRItem class generated two files: BNRItem.h and BNRItem.m . Locate those files in the project navigator. BNRItem.h is the header file (also called an interface file). This file declares the name of the new class, its superclass, the instance variables that each instance of this class has, and any methods this class implements. BNRItem.m is the implementation file, and it contains the code for the methods that the class implements. Every Objective-C class has these two files. You can think of the header file as a user manual for an instance of a class and the implementation file as the engineering details that define how it really works.
     
    Open BNRItem.h in the editor area by clicking on it in the project navigator. The file currently looks like this:
     
    #import

@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

Similar Books

Overdrive

Chloe Cole

Dream Paris

Tony Ballantyne

Crave

Jordan Sweet