Re: Style Advice for using NSEnumerator
Re: Style Advice for using NSEnumerator
- Subject: Re: Style Advice for using NSEnumerator
- From: Ignacio Enriquez <email@hidden>
- Date: Tue, 18 May 2010 04:21:18 +0900
I think is fine but If I were you I probably will try to find some kind of function that gives me the same result as using enumerators
For example in this case I will use UIColor colorWithRed:green:blue:alpha method and use the radius to determine components of a color
When numerator objects are just a few like this case is ok, but when you have many many possibilities finding a function is the best option you have.
(although there is no such a function sometimes)
I hope this helps.
Ignacio
Message: 5
Date: Mon, 17 May 2010 14:32:18 +0100
From: Carlton Gibson <email@hidden>
Subject: Style Advice for using NSEnumerator
To: Cocoa Developer <email@hidden>
Message-ID: <email@hidden>
Content-Type: text/plain; charset=us-ascii
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