Looking for a JavaEE Architect or Potential Tech Co-Founder?
Please don't hesitate to contact me.

iPhone command line tool development, working with framework classes

iPhone command line tool development, working with framework classes

I was studying iPhone when I found this video tutorial and online exercises from Stanford University. I think they offered this course last 2009.

So I'm gonna follow their videos, slides, assignments and exercises.

Assignment 1B: WhatATool
This exercise explores the following classes: NSString, NSURL, NSMutableArray, NSArray, NSProcessInfo, NSMutableString, etc. For a depth explanation of these classes consult the xcode documentation.

You should follow the instruction from the pdf available on Stanford site. Or simply create a new Foundation class project. See attached image.

I'll explain the exercise in code:
#import <Foundation/Foundation.h>

void PrintPathInfo() {
    //expand the directory
    NSString *path = [@"~" stringByExpandingTildeInPath];
    NSLog(@"My home folder is at %@", path);
    
    //get the components of the url
    NSArray *folders = [path pathComponents];
    
    //print each component/folder
    for(NSString *element in folders) {
        NSLog(@"%@", element);
    }
}

void PrintProcessInfo() {
    NSString *processName = [[NSProcessInfo processInfo] processName];
    NSInteger processIdentifier = [[NSProcessInfo processInfo] processIdentifier];
        
    //prints the process name and id
    NSLog(@"Process Name: '%@' Process ID: '%i'", processName, processIdentifier);
}

void PrintBookmarkInfo() {
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    //creates an NSURL object and add it to a NSMutableDictionary object
    [dictionary setValue: [NSURL URLWithString:@"http://www.stanford.edu"] forKey: @"Stanford University"];
    [dictionary setValue: [NSURL URLWithString:@"http://www.apple.com"] forKey: @"Apple"];
    [dictionary setValue: [NSURL URLWithString:@"http://www.cs193p.stanford.edu"] forKey: @"CS193P"];
    [dictionary setValue: [NSURL URLWithString:@"http://www.itunes.stanford.edu"] forKey: @"Stanford on iTunes U"];
    [dictionary setValue: [NSURL URLWithString:@"http://www.stanfordshop.com"] forKey: @"Stanford Mall"];
    
    //iterate
    NSString *key;
    for(key in dictionary) {
        NSString *value = [dictionary objectForKey:key];
        //checks if prefix has "Stanford" word
        if([key hasPrefix: @"Stanford"]) {
            NSLog(@"%@", value);
        }
    }
}

void PrintIntrospectionInfo() {
    //creates an assorted types of objects
    NSString *string = [NSString stringWithFormat:@"Hello World!"];
    NSURL *url = [NSURL URLWithString:@"http://google.com"];
    NSProcessInfo *processInfo = [NSProcessInfo processInfo];
    NSDictionary *dictionary = [NSDictionary dictionary];
    NSMutableString *ms1 = [NSMutableString string];
    [ms1 appendString:@"Hello "];
    [ms1 appendString:@"World!"];
    
    NSMutableArray *array = [NSMutableArray array];
    
    //add to an object array
    [array addObject: string];
    [array addObject: url];
    [array addObject: processInfo];
    [array addObject: dictionary];
    [array addObject: ms1];
    
    //iterate to the object array
    int count = [array count];
    for(int i = 0; i < count; i++) {
        //get the object at the current index
        NSObject *object = [array objectAtIndex: i];
        
        //prints the class name
        NSLog(@"Class name: %@", [object className]);
        
        //check if the object is a member of class NSString
        if([object isMemberOfClass: [NSString class]]) {
            NSLog(@"Is Member of NSString: YES");
        } else {
            NSLog(@"Is Member of NSString: NO");
        }
        
        //check if the object is a kind of NSString class
        if([object isKindOfClass: [NSString class]]) {
            NSLog(@"Is Kind of NSString: YES");
        } else {
            NSLog(@"Is Kind of NSString: NO");
        }
        
        //initialize a lowercaseString selector
        SEL selector = @selector(lowercaseString);
        //check if the object respond to that selector
        if([object respondsToSelector: selector]) {
            NSLog(@"Responds to lowercaseString: YES");
            //perform the method
            NSLog(@"lowercaseString is: %@", [object performSelector: selector]);
        } else {
            NSLog(@"Responds to lowercaseString: NO");
        }
        
        NSLog(@"==================================");
    }
}

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

    NSLog(@"Hello, World!");
    
    PrintPathInfo();
    PrintProcessInfo();
    PrintBookmarkInfo();
    PrintIntrospectionInfo();
    
    [pool drain];
    return 0;
}
iPhone command line tool development, working with framework classes iPhone command line tool development, working with framework classes Reviewed by Edward Legaspi on Wednesday, December 15, 2010 Rating: 5

No comments:

Powered by Blogger.