Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Objective-C.Programming.pdf
Скачиваний:
14
Добавлен:
21.02.2016
Размер:
8.64 Mб
Скачать

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

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]