Amazon.com Widgets Ben's Answer
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 19, 2013, 02:02:53 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Webcast Series Part II, Oct. 1 - 27
| | |-+  Example using hasPrefix: and hasSufix:
| | | |-+  Ben's Answer
Pages: [1]   Go Down
Print
Author Topic: Ben's Answer  (Read 735 times)
webwrx
Newbie
*
Posts: 41






« on: October 07, 2009, 03:43:10 PM »

I took the opportunity to also practice iterating through arrays.

Code: (Objective-C)
#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

Code: (Objective-C)
[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.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #1 on: October 07, 2009, 04:10:52 PM »

Looks good!  Of course, I haven't covered arrays yet in the series (that's tomorrow's--10/8--topic!).

Cheers,

Steve Kochan
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.