I took the opportunity to also practice iterating through arrays.
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *report1 = @"Fine in Sydney";
NSString *report2 = @"Cloudy in Hong Kong";
NSString *report3 = @"Fine in New York";
NSString *report4 = @"Rain in Paris";
NSString *report5 = @"Fine in Rio";
NSArray *weatherReports = [NSArray arrayWithObjects: report1, report2, report3, report4, report5, nil];
// List all locations with fine weather
NSLog (@"FINE WEATHER AT THESE LOCATIONS:");
for (NSString *report in weatherReports)
if ([report hasPrefix: @"Fine"] == YES)
NSLog (@"%@", report);
NSLog (@"---------------------------------");
// List the weather for Hong Kong only
NSLog (@"HONG KONG WEATHER:");
for (NSString *report in weatherReports)
if ([report hasSuffix: @"Hong Kong"] == YES)
NSLog (@"%@", report);
[pool drain];
return 0;
}
OUTPUT[Session started at 2009-10-08 09:33:51 +1100.]
2009-10-08 09:33:51.558 weather[48682:10b] FINE WEATHER AT THESE LOCATIONS:
2009-10-08 09:33:51.573 weather[48682:10b] Fine in Sydney
2009-10-08 09:33:51.573 weather[48682:10b] Fine in New York
2009-10-08 09:33:51.574 weather[48682:10b] Fine in Rio
2009-10-08 09:33:51.575 weather[48682:10b] ---------------------------------
2009-10-08 09:33:51.575 weather[48682:10b] HONG KONG WEATHER:
2009-10-08 09:33:51.576 weather[48682:10b] Cloudy in Hong Kong
The Debugger has exited with status 0.