01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

122 CHAPTER 5 Creating classes<br />

}<br />

cell.textLabel.text = address;<br />

NSString *imageName =<br />

[cityMappings objectForKey:city];<br />

cell.imageView.image =<br />

[UIImage imageNamed:imageName];<br />

cell.detailTextLabel.text =<br />

[NSString<br />

stringWithFormat:@"Rents for $%0.2f per week",<br />

property.rentalPrice];<br />

return cell;<br />

- (void)dealloc {<br />

[cityMappings release];<br />

[properties release];<br />

[super dealloc];<br />

}<br />

@end<br />

The first change found in RootViewController.m is the importation of the CTRental-<br />

Property.h header file B via an #import statement. This statement requests that the<br />

<strong>Objective</strong>-C compiler read the contents of the specified file and interpret it as if its contents<br />

were written directly at the point of the #import statement. In other words, this<br />

line makes the compiler aware of the definition of the CTRentalProperty class and<br />

allows you to use it. Without this line, the compiler wouldn’t know about the existence<br />

of the CTRentalProperty class—at least, not while compiling RootViewController.m.<br />

e<br />

Release the<br />

memory<br />

d<br />

Update<br />

the<br />

method<br />

What about #include? What’s this nonstandard #import?<br />

If you have knowledge of C or C++, you may know that C-based languages traditionally<br />

use an #include statement to include the contents of another file. <strong>Objective</strong>-C, by<br />

contrast, introduced and recommends the use of the #import statement.<br />

The difference in behavior is subtle. If two #include statements specify the same<br />

file, the compiler will probably complain that it has seen multiple definitions for the<br />

classes and type definitions found in the file. This is because the #include statement<br />

reads the contents of the specified file and passes it to the <strong>Objective</strong>-C compiler.<br />

With multiple #include statements for the same file, the compiler sees the<br />

source code multiple times.<br />

The #import statement, on the other hand, has slightly more smarts to it. If a second<br />

#import statement is found for the same file, it is, in effect, ignored. This means the<br />

contents of the header file are seen only once by the compiler, no matter how many<br />

times they’re requested to be imported.<br />

#import is similar to #pragma once and other such solutions that many C and C++<br />

compilers support as extensions to provide similar convenience.<br />

The next major change is found in viewDidLoad c. When your Rental Manager<br />

application first becomes visible, you create a new NSArray object and populate it with

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!