Style Advice for using NSEnumerator
Style Advice for using NSEnumerator
- Subject: Style Advice for using NSEnumerator
- From: Carlton Gibson <email@hidden>
- Date: Mon, 17 May 2010 14:32:18 +0100
Hi All,
I'm still coming to grips with Cocoa and I'm wondering if any list members would be kind enough just to review a small code fragment for style advice...
The following occurs as part of the drawRect method of a UIView subclass. The method simply draws concentric circles inside the view. (It's part of an exercise from the Big Nerd Ranches iPhone Programming book, which I'm enjoying a lot.)
Originally the routine simply drew the circles in a single color but the book sets the problem of having it draw them in assorted colors so I came up with this:
// Set up the stroke colors (and the variables for enumeration)
NSArray *strokeColors = [NSArray arrayWithObjects:
@"redColor", @"orangeColor", @"yellowColor", @"greenColor",
@"blueColor", @"cyanColor", @"magentaColor", nil];
NSEnumerator *enumerator = nil;
NSString *colorString;
// Draw concentric circles from the outside in
for (float currentRadius = maxRadius;
currentRadius > 0;
currentRadius -= 20)
{
// Determine stroke color
if (!(colorString = [enumerator nextObject])) {
enumerator = [strokeColors objectEnumerator];
colorString = [enumerator nextObject];
}
SEL selector = NSSelectorFromString(colorString);
[[UIColor performSelector:selector] setStroke];
// Do Drawing
CGContextAddArc(context, center.x, center.y,
currentRadius, 0.0, M_PI * 2.0, YES);
CGContextStrokePath(context);
}
My question concerns the use the NSEnumerator here. The code works, but is it _right_? In particular, I'm slightly nervous about relying on the enumerator being nil first time through the loop, and on testing the return value of nextObject in the conditional -- perhaps though this is just an idiom I need to get used to?
Any thoughts from more seasoned Cocoa hands as to how to handle this sort of situation would be appreciated! :-)
Thanks in advance.
Regards,
Carlton_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden