Greg’s Online CS 193P Resources

April 8, 2009

My Assignment 1B Solution

Filed under: cs 193p,iPhone — gregorykaiser @ 7:46 pm

Assignment 1A is a follow along exercise. Assignment 1B “WhatATool” Part 1 took me a long time as I was learning Obj. C. The code below should be saved into “WhatATool.m”. Put the file in the xcode project directory after the project is set up as explained in the assignment you should see the correct output in the xcode console. I hope this saves you some time!

#import <Foundation/Foundation.h>

// sample function for one section, use a similar function per section - copied from assignment sheet
void PrintPathInfo() { 
	// Code from path info section here 
	NSLog(@"Enter PrintPathInfo");
	NSString *aString = @"My Home Folder is at:";
	NSString *path = @"~";
	path = [path stringByExpandingTildeInPath];
	NSLog([aString stringByAppendingString: path]);
	NSInteger i = 0;
	NSArray *pathComponents = [path pathComponents];
	for (NSString *element in pathComponents) {
		NSLog(@"element %d: %@", i, element);

void PrintProcessInfo() { 
	// Code 
	NSLog(@"Enter PrintProcessInfo");
	NSString *processName = [[NSProcessInfo processInfo] processName];
	// NSLog(processName);
	NSInteger processID  = [[NSProcessInfo processInfo] processIdentifier];
	// NSLog( @"%i", processID);
	NSLog(@"Process Name: %@    Process ID: %d  ", processName, processID);
void PrintBookmarkInfo() { 
	// Code 
	NSLog(@"Enter PrintBookmarkInfo");
	NSMutableDictionary *bkmks = [NSMutableDictionary dictionaryWithCapacity:10];
	// NSURL *site = [ NSURL URLWithString: @""];
	[ bkmks setObject:[ NSURL URLWithString: @""] forKey:@"Stanford University"];
	[ bkmks setObject:[ NSURL URLWithString: @""] forKey:@"Apple"];
	[ bkmks setObject:[ NSURL URLWithString: @""] forKey:@"CS193P"];
	[ bkmks setObject:[ NSURL URLWithString: @""] forKey:@"Stanford on iTunes U"];
	[ bkmks setObject:[ NSURL URLWithString: @""] forKey:@"Stanford Mall"];
	for (id key in bkmks) {
		if ([key hasPrefix:@"Stanford"])
			NSLog(@"Key: '%@', URL: '%@'", key, [bkmks objectForKey:key]);

void PrintIntrospectionInfo() { 
	// Code 
	NSLog(@"Enter PrintIntrospectionInfo");
	NSString *path = @"~";
	path = [path stringByExpandingTildeInPath];
	NSArray *pathComponents = [path pathComponents];
	NSString *processName = [[NSProcessInfo processInfo] processName];
	// NSInteger processID  = [[NSProcessInfo processInfo] processIdentifier];	
	NSURL *site = [ NSURL URLWithString: @""];
	NSMutableDictionary *bkmks = [NSMutableDictionary dictionaryWithCapacity:10];	
	NSMutableString *mstring1 = @"Mutable1";
	NSMutableString *mstring2 = @"THIS WAS ALL CAPS";
	NSMutableArray *marray = [NSMutableArray arrayWithCapacity:10];
	[marray addObject:path];
	[marray addObject:pathComponents];
	[marray addObject:processName];
	// adding NSInteger to mutable array causes warning that it is being changed to a pointer (??)
	// [marray addObject:processID];
	[marray addObject:site];
	[marray addObject:bkmks];
	[marray addObject:mstring1];
	[marray addObject:mstring2];
	// NSLog(@" %@", marray);

	NSUInteger limit = [marray count];
	NSUInteger i = 0;
	SEL sel = @selector(lowercaseString);
	for (i=0 ; i <  limit ; i++) {
		NSLog(@"Class Name:%@ ", &#91;&#91;marray objectAtIndex:(unsigned)i&#93; className&#93;);
		NSLog(@"Is Member of NSString:%@ ", (&#91;&#91;marray objectAtIndex:(unsigned)i&#93; isMemberOfClass:&#91;NSString class&#93;&#93; ? @"YES" : @"NO"));
		NSLog(@"Is Kind of NSString:%@ ", (&#91;&#91;marray objectAtIndex:(unsigned)i&#93; isKindOfClass:&#91;NSString class&#93;&#93; ? @"YES" : @"NO" ));
		// NSLog(@" sel: %@", sel);
		if (&#91;&#91;marray objectAtIndex:(unsigned)i&#93; respondsToSelector:sel&#93;) 
			NSLog(@"Responds to lowercaseString: YES");
			NSLog(@"lowercaseString is: %@", &#91;&#91;marray objectAtIndex:(unsigned)i&#93; performSelector:sel withObject:&#91;marray objectAtIndex:(unsigned)i&#93; &#93;);
		} else
			NSLog(@"Responds to lowercaseString: NO");

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

    // insert code here...
	NSLog(@"================WhatATool(Part 1)===========================");
	//code for cs193p
	PrintPathInfo();           // Section 1 
	PrintProcessInfo();        // Section 2 
	PrintBookmarkInfo();       // Section 3 
	PrintIntrospectionInfo();  // Section 4 

    [pool drain];  //drain or release?? I think drain - see above document
    return 0;



  1. HI greg, I know I’m late to the party. I’m glad to have found your blog, because I know I’m going to need some help with the later assignments, since I’ve spent a week on Assignment 1B šŸ™‚ Well, working 30 minutes or so per evening over 2 or 3 evenings. Thanks for all your efforts – I’ll be appreciating them soon! šŸ™‚

    I’ve been trying hard not to look at your solutions before I have a go at mine. But I may weaken in time.

    Comment by 24hourtime — May 15, 2009 @ 5:55 pm | Reply

  2. Keep at it as the CS193P class is very good. And make sure and download the class materials before it ends as they may go away. Finally, thanks for your kind words the point of the blog is to help us on line students make headway in the class.



    Comment by gregorykaiser — May 15, 2009 @ 6:05 pm | Reply

  3. thanks!

    Comment by scott — May 23, 2009 @ 4:52 am | Reply

  4. Very useful – Thanks.

    Comment by Lui-g — June 3, 2009 @ 8:21 pm | Reply

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at

%d bloggers like this: