Skip to content

Instantly share code, notes, and snippets.

@glongman
Created September 14, 2009 13:27
Show Gist options
  • Save glongman/186644 to your computer and use it in GitHub Desktop.
Save glongman/186644 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
/*
* call:
* PrintPathInfo();
*
* params:
* None.
*
* returns:
* nothing (void?)
*
* Excercises NSString methods for manipulating file path.
* Logs the location of the current User's home directory
* and then enumerated a list of the components of that path.
*/
void PrintPathInfo() {
NSString *path = [@"~" stringByExpandingTildeInPath];
NSLog(@"My home folder is at '%@'", path);
for (id component in [path pathComponents]) {
NSLog(@"%@", component);
}
}
/*
* call:
* PrintProcessInfo();
*
* params:
* None.
*
* returns:
* nothing (void?)
*
* Finds out a bit about this own process using NSProcessInfo
* Logs the process name and process identifier like this:
* example:
* 12/09/09 4:18:03 PM WhatATool[13207] Process Name: 'WhatATool' Process Identifier: 13207
*/
void PrintProcessInfo() {
NSProcessInfo *process_info = [NSProcessInfo processInfo];
NSLog(@"Process Name: '%@' Process Identifier: %d", [process_info processName], [process_info processIdentifier]);
}
/*
* call:
* PrintBookmarkInfo();
*
* params:
* None.
*
* returns:
* nothing (void?)
*
* Play with NSMutableDictionay.
* Logs urls stored in the dictionary that have keys that
* begin with 'Stanford'.
*/
void PrintBookmarkInfo() {
NSMutableDictionary *bookmarks = [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];
for (NSString *key in bookmarks) {
if ([key hasPrefix:@"Stanford"]) {
NSLog(@"Key: '%@' URL: '%@'", key, [bookmarks objectForKey:key]);
}
}
}
/*
* call:
* PrintIntrospectionInfo();
*
* params:
* None.
*
* returns:
* nothing (void?)
*
* Play with Introspection.
* Logs answers to the following questions about various objects
* example output:
* 2008-01-10 20:56:03 WhatATool[360] Class name: NSCFString
* 2008-01-10 20:56:03 WhatATool[360] Is Member of NSString: NO
* 2008-01-10 20:56:03 WhatATool[360] Is Kind of NSString: YES
* 2008-01-10 20:56:03 WhatATool[360] Responds to lowercaseString: YES
* 2008-01-10 20:56:03 WhatATool[360] lowercaseString is: hello world!.
*/
void PrintIntrospectionInfo() {
NSMutableArray *bucket = [NSMutableArray arrayWithCapacity:10];
[bucket addObjectsFromArray:[NSArray arrayWithObjects:
[NSURL URLWithString:@"http://www.stanford.edu"],
@"Stanford University",
[NSDictionary dictionaryWithObjectsAndKeys:@"http://www.stanford.edu",@"Stanford University", nil],
[NSProcessInfo processInfo],
[[NSMutableString alloc] initWithString: @"1st Mutable String"],
[[NSMutableString alloc] initWithString: @"2nd Mutable String"],
nil]];
for( id obj in bucket) {
NSLog(@"Class Name: %@", [obj className]);
NSLog(@"Is Member of NSString: %@", ([obj isMemberOfClass:[NSString class]] == YES ? @"YES" : @"NO"));
NSLog(@"Is Kind of NSString: %@", ([obj isKindOfClass:[NSString class]] == YES ? @"YES" : @"NO"));
int responds = [obj respondsToSelector:@selector(lowercaseString)];
NSLog(@"Responds to lowercaseString: %@", (responds == YES ? @"YES" : @"NO"));
if (responds == YES) {
NSLog(@"lowercaseString is: '%@'", [obj lowercaseString]);
}
NSLog(@"======================================");
}
}
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog(@"======== Section 1 =========");
PrintPathInfo();
NSLog(@"======== Section 2 =========");
PrintProcessInfo();
NSLog(@"======== Section 3 =========");
PrintBookmarkInfo();
NSLog(@"======== Section 4 =========");
PrintIntrospectionInfo();
NSLog(@"done.");
[pool drain];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment