Professional Documents
Culture Documents
1. I want to add a new method to the TADCar class that will return the manufacturer
name of the car. What would that method definition look like in the TADCar .h file?
-(NSString *)manufacturer;
The name of the method, manufacturer, isn’t important here. You could have also
named it manufacturerName or something else. What’s important is the return value
that the method returns and the input arguments that the method receives. Since we’re
asking the car for it’s manufacturer name, it requires no arguments. We’ll assume that
when we write the method, the manufacturer name be given back in the form of an
NSString. So we make NSString as the return value. Later, when we want to call this
method on a car object, we would type something like this:
This would store the manufacturer name of the car in the NSString variable
“manufacturerName”.
2. I want to add a new method to the TADCar class that will set the number of
passengers in the car. What would that method definition look like in the .h file?
-(void)setNumberOfPassengers:(int)passengers;
The “void” in parenthesis indicates that there is no return value. The “(int)passengers”
indicates that the method accepts a an integer or int type as the argument. Note that
it’s not “(int *)passengers” because an int is just a type and not an object. We could call
this method on our car object like this:
[car setNumberOfPassengers:3];
The first line calls the method driverName on the car object. We know that driverName
returns an NSString object because it’s method definition looks like this in the TADCar.h
file:
-(NSString *)driverName;
The next line calls the method ‘length’ in the NSString object savedName. How do we
know an NSString object can accept the length method? Since NSString is one of
Apple’s classes, we can look it up in the documentation by option+clicking on NSString
in our code. Then we scan the list of method until we see one that might do what we
want. The documentation states that the length method “returns the number of Unicode
characters in the receiver.” That’s exactly what we want. Note that the documentation
states that the method returns an NSUInteger value. The int type will work here in place
of an NSUInteger, but you could also write the second line like this if you wanted: