#import <
#import <
void PrintPathInfo() {
// Code from path info section here
NSLog(@"Printing Path");
NSString *path = @"~";
NSString *adjusted=[path stringByExpandingTildeInPath];
NSLog(adjusted);
NSLog([NSString stringWithFormat:@"My home folder is at '%@'",adjusted]);
NSArray *pathComponents = [adjusted pathComponents];
NSLog(@"Path Components Next");
for (NSString *pc in pathComponents) {
NSLog(pc);
}
}
void PrintProcessInfo() {
// Code from path info section here
NSLog(@"Printing PrintProcessInfo");
NSString *processName = [[NSProcessInfo processInfo] processName];
int processIdentifier = [[NSProcessInfo processInfo] processIdentifier];
NSString *answer = [NSString stringWithFormat:@"Process Name: %@ Process ID: %d", processName, processIdentifier];
NSLog(answer);
//NSLog(processName);
}
void PrintBookmarkInfo() {
NSLog(@"PrintBookmarkInfo()");
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:5];
[dict setObject: [NSURL URLWithString:@"http://www.stanford.edu"] forKey: @"Stanford University"];
[dict setObject:[NSURL URLWithString:@"http://www.apple.com"] forKey: @"Apple"];
[dict setObject: [NSURL URLWithString:@"http://cs193p.stanford.edu"] forKey:@"CS193P"] ;
[dict setObject: [NSURL URLWithString:@"http://itunes.stanford.edu"] forKey: @"Stanford on iTunes U"];
[dict setObject: [NSURL URLWithString:@"http://stanfordshop.com"] forKey:@"Stanford Mall"] ;
NSArray *keys;
keys = [dict allKeys];
for(NSString *key in keys) {
if([key hasPrefix:@"Stanford"]) {
NSURL *url = [dict objectForKey:key];
NSLog([NSString stringWithFormat:@"Key: %@ URL: %@",key,url]);
}
}
}
void PrintIntrospectionInfo() {
NSLog(@"PrintIntrospectionInfo");
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:4];
[arr addObject:@"John G. Kroubalkian"];
[arr addObject:@"2John G. Kroubalkian"];
[arr addObject:@"3John G. Kroubalkian"];
[arr addObject:@"4John G. Kroubalkian"];
int count = [arr count];
for (int i = 0 ; i <>
NSObject *obj = [arr objectAtIndex:i];
//NSLog([NSString stringWithFormat:@"%@",obj]);
Class claz = [obj class];
NSString *className = [claz description];
NSLog([NSString stringWithFormat:@"Class name: %@",className]);
if([[arr objectAtIndex:i] isMemberOfClass:[NSString class]]){
NSLog(@"Is Member of NSString: YES");
}else{
NSLog(@"Is Member of NSString: NO");
}
if([[arr objectAtIndex:i] isKindOfClass:[NSString class]]){
NSLog(@"Is Kind of NSString: YES");
}else{
NSLog(@"Is Kind of NSString: NO");
}
//NSLog([NSString stringWithFormat: @"Is Member of NSString: %@",memberOfClassNsString]);
SEL sel = @selector(lowercaseString);
if ([obj respondsToSelector:sel]) {
NSLog(@"Responds to lowercaseString: YES");
NSString *res = [NSString stringWithFormat:@"lowercaseString is: %@", [obj performSelector:sel withObject:obj]];
NSLog(res);
}else{
NSLog(@"Responds to lowercaseString: NO");
}
}
}
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
PrintPathInfo();
PrintProcessInfo();
PrintBookmarkInfo();
PrintIntrospectionInfo();
[pool drain];
}
Thanks for posting this! It certainly helped me with this assignment in a few places. Here's my version in case anyone wants it as reference (import Foundation/Foundation.h and Cocoa/Cocoa.h):
ReplyDelete//section 1
void PrintPathInfo() {
NSString *path = @"~";
path = [path stringByExpandingTildeInPath];
NSArray *arr = [path pathComponents];
for( NSString* oneString in arr){
NSLog( [oneString stringByAppendingString:@"\n"] );
}
}
//section 2
void PrintProcessInfo() {
NSLog(@"Process Name: %@ Process ID: %d",[[NSProcessInfo processInfo] processName],[[NSProcessInfo processInfo] processIdentifier]);
}
//section 3
void PrintBookmarkInfo(){
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:5];
[dict setObject: [NSURL URLWithString:@"http://www.stanford.edu"] forKey: @"Stanford University"];
[dict setObject: [NSURL URLWithString:@"http://stanfordshop.com"] forKey: @"Stanford Mall"];
[dict setObject: [NSURL URLWithString:@"itunes.stanford.edu"] forKey: @"Stanford on iTunes U"];
[dict setObject: [NSURL URLWithString:@"apple.com"] forKey: @"Apple"];
[dict setObject: [NSURL URLWithString:@"CS193p"] forKey: @"CS193p.stanford.edu"];
for(NSString *key in [dict allKeys]){
if([key hasPrefix:(@"Stanford")]){
NSLog(@"Key: '%@' URL: '%@'",key,[[dict objectForKey:key] absoluteString]);
}
}
}
//section 4
void PrintIntrospectionInfo(){
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:4];
[arr addObject:[NSString stringWithString:@"TEST STRING AHDJKSADFLHS"]];
[arr addObject:[NSURL URLWithString:@"http://www.cnn.com"]];
[arr addObject:[NSProcessInfo processInfo]];
[arr addObject:[NSMutableDictionary dictionaryWithCapacity:10]];
NSString *temp = @"NO";
SEL sel = @selector(lowercaseString);
BOOL respond;
for(int i=0; i<4;i++){
temp = @"NO";
respond = NO;
NSLog(@"Class name: %@",[[[arr objectAtIndex:i] class] description]);
if([[arr objectAtIndex:i] isMemberOfClass:[NSString class]]){
temp = @"YES";
}
NSLog(@"Is Member of NSSTring: %@",temp);
temp=@"NO";
if([[arr objectAtIndex:i] isKindOfClass:[NSString class]]){
temp = @"YES";
}
NSLog(@"Is Kind of NSSTring: %@",temp);
temp=@"NO";
if([[arr objectAtIndex:i] respondsToSelector:sel]){
temp = @"YES";
respond = YES;
}
NSLog(@"Responds to lowercaseString: %@",temp);
if(respond){
NSLog(@"To Lowercase: %@",[[arr objectAtIndex:i] performSelector:sel]);
}
}
}
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
PrintPathInfo(); // Section 1
PrintProcessInfo(); // Section 2
PrintBookmarkInfo(); // Section 3
PrintIntrospectionInfo(); // Section 4
[pool release];
return 0;
}
Just started doing these assignments and found this site... awesome work... was curious with this assignment, is there no way to get everything done in section 4 for the "isMemberOfClass" on 1 line... Such as...
ReplyDeleteNSLog(@"Is Member of NSString: %@", [element isMemberOfClass:[NSString class]]);
Just curious, definitely enjoy keeping my code clean is all :D
You could use the ternary operator to keep it all on one line:
ReplyDeleteNSLog(@"Is a member of NSString: %@", [element isMemberOfClass:[NSString class]] ? @"Yes" : @"No");
The code here helped me with my selector, which I do not fully understand my I add. Also to tidy up the section 4 as originally my code was like this -
ReplyDelete//NSString *myString = @"Hello World!";
//NSMutableDictionary *myDictionary = [NSMutableDictionary dictionary];
//NSURL *myURL = @"www.shlong.com";
//NSNumber *myNumber = [NSNumber numberWithInt:10];
//NSMutableArray *myArray = [NSMutableArray arrayWithObjects:myURL, myNumber, myDictionary, myString, nil];
And I found the code here a bit cleaner, so I incorporated the manner in which it was written it.
MY CODE IS HERE ON MY BLOG -> http://nates-blog-spot.blogspot.com/
Wow, you guys are excessive. How about just this:
ReplyDeleteNSMutableArray *objects = [NSArray arrayWithObjects: [[NSMutableString alloc] initWithString: @"Howdy"], [NSNumber numberWithInt:15], @"CAPS_LOCK", [NSURL URLWithString:@"http://www.apple.com"], [NSArray arrayWithObjects: @"1", @"2", @"3", nil], [NSDictionary class], [NSMutableString stringWithFormat:@"What?"], [NSProcessInfo class], @"the END?", nil];
for (int i=0; i<[objects count]; i++)
{
NSObject *obj = [objects objectAtIndex:i];
SEL sel = @selector(lowercaseString);
BOOL ismemberofstring = [obj isMemberOfClass:[NSString class]];
BOOL iskindofstring = [obj isKindOfClass:[NSString class]];
BOOL canlowercasestring = [obj respondsToSelector:sel];
NSLog(@"Class: '%@'", [obj className]);
NSLog(@"Is Member of NSString: %@", ismemberofstring ? @"YES" : @"NO");
NSLog(@"Is Kind of NSString: %@", iskindofstring ? @"YES" : @"NO");
NSLog(@"Responds to lowercaseString: %@", canlowercasestring ? @"YES" : @"NO");
if (canlowercasestring)
NSLog(@"lowercaseString is: %@", [obj performSelector:sel withObject: obj]);
NSLog(@"======================================");
}
Has anyone ever found a case where 'isMemberOfClass' is true?!? If someone has an example of where this comes out 'YES' please let me know.
ReplyDeleteI'm just entering these waters but from what i found so far is that when you get a NO returned from isMemeberOfClass, subsitute isKindOfCLass and you will be returned a YES. I read some tings about clusters and what not, but all I want ot do is creat some apps:>) so I am not going ot lose any sleep over it, but chals it up to oh well here's a quirk
ReplyDeleteI used a bit shorter code to do the PrintProcessInfo section of the assignment:
ReplyDeletevoid PrintProcessInfo(){
int pathID;
NSProcessInfo *proc=[NSProcessInfo processInfo];
pathID=[proc processIdentifier];
NSLog(@"Process Name: %@, Process ID: %i",[proc processName],pathID);
}
anchorbeach why have two lines of code to define pathID when you could just put it inline like you did for processName?
ReplyDeletevoid PrintProcessInfo() {
NSProcessInfo *process = [NSProcessInfo processInfo];
NSLog([NSString stringWithFormat:@"Process Name: '%@' Process ID: '%d'", process.processName, process.processIdentifier]);
}
NSArray *keys;
ReplyDeletekeys = [dict allKeys];
for(NSString *key in keys)
should be written as
for(NSString *key in dict)