スタンフォード大学のiPhoneアプリ開発講座の宿題(その2)

宿題その2(Command Line Tool I)の自分なりの回答を以下に記載しておく(宿題その1は非常に簡単なので回答の記載はスキップした)。

#import <Foundation/Foundation.h>

void PrintPathInfo() {
    NSLog(@"--- Section 1 ---");

    // Expand the tilde in the path to the full path
    // to your home directory.
    NSString *path = @"~";
    path = [path stringByExpandingTildeInPath];
    NSLog(@"My home folder is at '%@'", path);

    // Get an array of path components
    NSArray *array = [path pathComponents];
    for (NSString *element in array) {
        NSLog(element);
    }
}

void PrintProcessInfo() {
    NSLog(@"--- Section 2 ---");

    // Get the name and process identifier (pid) of the process
    NSProcessInfo *processInfo = [NSProcessInfo processInfo];
    NSLog(@"Process Name: '%@' Process ID: '%d'",
                [processInfo processName],
                [processInfo processIdentifier]);
}

void PrintBookmarkInfo() {
    NSLog(@"--- Section 3 ---");

    // Create a mutable dictionary
    NSMutableDictionary *dictionary =
        [NSMutableDictionary dictionaryWithObjectsAndKeys:
        [NSURL URLWithString:@"http://www.stanford.edu"],
        @"Stanford University",
        [NSURL URLWithString:@"http://www.apple.com"],
        @"Apple",
        [NSURL URLWithString:@"http://cs193p.stanford.edu"],
        @"CS193P",
        [NSURL URLWithString:@"http://itunes.stanford.edu"],
        @"Stanford on iTunes U",
        [NSURL URLWithString:@"http://stanfordshop.com"],
        @"Stanford Mall",
        nil];
   
    // Enumerate through the keys of the dictionary
    // If the key does not start with @"Stanford", no output is printed
    for (NSString *key in dictionary) {
        if ([key hasPrefix:@"Stanford"]) {
            NSLog(@"Key: '%@' URL: '%@'", key,
                            [dictionary objectForKey:key]);
        }
    }
}

void PrintIntrospectionInfo() {
    NSLog(@"--- Section 4 ---");

    // Testing Selectors, Classes and Introspection
    NSString *string = @"Hello World!";
    NSURL *url = [NSURL URLWithString:@"http://www.apple.com"];
    NSDictionary *dictionary =
        [NSMutableDictionary dictionaryWithObjectsAndKeys:
        [NSURL URLWithString:@"http://www.stanford.edu"],
        @"Stanford University",
        [NSURL URLWithString:@"http://www.apple.com"],
        @"Apple",
        nil];
    NSMutableArray *array =
        [NSMutableArray arrayWithObjects:string, url, dictionary, nil];

    for (NSObject *object in array) {
        NSLog(@"Class name: %@", [object className]);
        NSLog(@"Is Member of NSString: %@",
            [object isMemberOfClass:[NSString class]] ? @"YES" : @"NO");
        NSLog(@"Is Kind of NSString: %@",
            [object isKindOfClass:[NSString class]] ? @"YES" : @"NO");
        if ([object respondsToSelector:@selector(lowercaseString)]) {
            NSLog(@"Responds to lowercaseString: YES");
            NSLog(@"lowercaseString is: %@",
                [object performSelector:@selector(lowercaseString)]);
        } else {
            NSLog(@"Responds to lowercaseString: NO");
        }
        NSLog(@"======================================");
    }
}

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    PrintPathInfo();
    PrintProcessInfo();
    PrintBookmarkInfo();
    PrintIntrospectionInfo();

    [pool drain];
    return 0;
}

以下のサイトでも回答が紹介されており、より詳しく解説されているので参考までに。