New to Objective C - what is the problem with this?

Joined
Apr 21, 2009
Messages
1
Reaction score
0
Points
1
Location
Lexington, MA
Your Mac's Specs
Macbook Pro 17 w/250GB SSD drive, 4GB running VMWare Fusion; iPhone 3G
Taking the Stanford U iPhone developer class & starting work on the lessons. Below is a simple introspection function to examine various classes. Question is: Why does uncommenting the one line with the NSArray cause the function to recurse/repeat numerous times through all the objects, while leaving it commented out makes it run as expected (one pass through the list of objects)? In fact, there are several other functions I call from main(), and their objects will show up in the output as well if I uncomment the NSArray line! I assume this is a pointer/bounds problem, but I don't know enough yet to figure out where the problem is in my syntax.

Also, a little amazed so much code is required to set up an array initialized with integers - any faster way?

Thanks for your help!

Code:
void PrintIntrospectionInfo() {
	NSLog(@" ");
	NSLog(@"*** PrintIntrospectionInfo ***");
	SEL mysel = @selector(lowercaseString);
	NSMutableArray *myObjs = [NSMutableArray arrayWithObjects:
		[NSMutableString stringWithFormat:@"this is test# %d", 12],
		@"foo",
		[NSArray arrayWithObjects:@"hello", @"there", nil],
		[NSURL URLWithString:@"http://www.abc.com/headlines"],
		[NSProcessInfo processInfo],
//		[NSArray arrayWithObjects:[NSNumber numberWithInt:5], [NSNumber numberWithInt:4], [NSNumber numberWithInt:3], nil],
		[NSMutableDictionary dictionaryWithObjectsAndKeys:
			[NSURL URLWithString:@"http://www.stanford.edu"],     @"Stanford University",
			[NSURL URLWithString:@"http://www.apple.com"],        @"Apple",
			[NSURL URLWithString:@"http://cs193p.stanford.edu"],  @"CS193P",
			nil]
	];
	for (id key in myObjs) {
		NSLog(@"Class Name: %@", [key className]);
		if ([key isKindOfClass:[NSArray class]]) {
			NSLog(@"Array size is: %d", [key count]);
		} else {
			NSLog(@"Object value: '%@'", key);
		}
		NSLog(@"Is member of NSString: %s", ([key isMemberOfClass:[NSString class]] ? "YES" : "NO"));
		NSLog(@"Is kind of NSString: %s", ([key isKindOfClass:[NSString class]] ? "YES" : "NO"));
		NSLog(@"Responds to lowercaseString: %s", ([key respondsToSelector:mysel] ? "YES" : "NO"));
		if ([key respondsToSelector:mysel])
			NSLog(@"lowercaseString is: '%@'", [key performSelector:mysel]);
		NSLog(@"===============================================");
	}
}  //PrintIntrospectionInfo
 
Joined
Mar 9, 2004
Messages
9,065
Reaction score
331
Points
83
Location
Munich
Your Mac's Specs
Aluminium Macbook 2.4 Ghz 4GB RAM, SSD 24" Samsung Display, iPhone 4, iPad 2
Hey! I've been looking at the Stanford classes as well... they're pretty good!
Perhaps you'd want to try using an arrayEnumurator to iterate over the objects instead?

Not sure though, I can't remember how I did it exactly (don't have my code here)...
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top