Crash when loading HypnosisView


#1

I have successfully made it to the point where I add the HypnosisView to the window, and I ran into a strange error that was making my app crash upon loading. Here is a chunk of my code from Hypnosis.m:

[code]// Draw concentric circles from the outside in
NSInteger count=0;

// Colors Challenge
NSArray *colors = [[NSArray alloc] initWithObjects:[UIColor lightGrayColor],
				   [UIColor magentaColor],
				   [UIColor purpleColor],
				   [UIColor cyanColor],
				   [UIColor brownColor],
				   nil];
for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20)
{
	NSLog(@"Radius:%@",currentRadius);
	[[colors objectAtIndex:(count%[colors count])] setStroke];
	CGContextAddArc(context, center.x, center.y, currentRadius, 0.0, M_PI * 2.0, YES);
	CGContextStrokePath(context);
	count++;
}
[colors release];[/code]

As you can see, I have an NSLog statement in that for loop, and I had it in there just for my own sanity check as I was playing around in the previous chapter. I’ve figured out that the crash stated above will go away if I simply comment out that NSLog(@“Radius:%@”,currentRadius) statement. Really, the app doesn’t crash as long as that NSLog statement isn’t trying to reference the currentRadius variable.

Why is this the case? I can’t seem to figure out why it would crash when trying to reference that variable and write it to the console. Isn’t it fully contained within the HypnosisView.m implementation?

Thanks ahead of time for the tips. I’m loving the book!
Paul


#2

Hi,

Not sure if this would stop the crash but have you tried

  NSLog(@"Radius:%f",currentRadius);

%@ is an object formatter

Gareth


#3

Yes, you must use %f for a floating point primitive (%d for an integer).

%@ is for objects only, when this token is encountered, the corresponding argument in NSLog is sent the message description and the NSString result is appended to the output string. If you try sending a message to a float, you get a crash.


#4

Yep, that did the trick. I should’ve known that, I’ve just gotten in the habit of spitting out my own debugging strings to the Console and got lazy with it. Thanks!