2/22/2010

Cocoa Maker 4 - Loops and Goto

James (MrGeckosMedia.com), joined by, Eduardo (MegaEduX.com), and Karl, teaches how loops, arguments, and goto works in Cocoa.

15:35


Example code from episode.
Loops Example
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    int i=0;
    while (i!=2) {
        NSLog(@"%d", i);
        i++;
    }
    i=0;
    do {
        NSLog(@"%d", i);
        i++;
    } while (i!=3);
    
    NSArray *anArray = [NSArray arrayWithObjects:@"Apples", @"Oranges", @"Grapes", @"Bananas", nil];
    for (int d=0; d<[anArray count]; d++) {
        NSLog(@"%@", [anArray objectAtIndex:d]);
    }
    
    NSString *theObject;
    for (theObject in anArray) {
        NSLog(@"For In: %@", theObject);
    }
    
    NSEnumerator *theEnumerator = [anArray objectEnumerator];
    while (theObject = [theEnumerator nextObject]) {
        NSLog(@"Enumerator: %@", theObject);
    }
    
    [pool drain];
    return 0;
}
Goto Example
#import <Foundation/Foundation.h>

void testGoto() {
    NSString *theString = [NSString new];
    if (1==12) {
        goto Error;
    }
    goto Cleanup;
Error:
    NSLog(@"An error occurred");
    goto Cleanup;
Cleanup:
    NSLog(@"Releasing the string");
    [theString release];
}

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    testGoto();
    
    [pool drain];
    return 0;
}

Keynote used in this Episode
Keynote in PDF Format

No comments:

Post a Comment