Вы находитесь на странице: 1из 2

Lesson 3 Challenge Solution

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?

Solution: The method definition would be something like this:

-(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:

NSString *manufacturerName = [car manufacturer];

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?

Solution: In question 1, we defined a method that accepted no arguments, but had a


return value. The method definition in question 2 is the opposite. We’ll accept a
number of passengers and we’ll return nothing. That method definition would look like
this:

-(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];

3. Using our car object in the Lesson3Project, I want to:


--store the driver name in a variable called savedName
--then find out how many characters are in the
driver's name
--Then store the number of characters in an integer
variable called numberOfCharacters

What code would I type to do that?

Solution: Here’s the code we would type:

NSString *savedName = [car driverName];


int numberOfCharacters = [savedName length];

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:

NSUInteger numberOfCharacters = [savedName length];

Вам также может понравиться