PDA

View Full Version : Simple Question About Fast Enumeration




Spike099
Apr 14, 2008, 09:57 PM
So, I was wondering if interrupting the fast enumeration process would pose any memory issues? Basically, I am wondering if the following code follows proper conventions. Where, as soon as a newsItem that has not beed read is found, the method returns YES.

- (BOOL)hasUnreadNews {
for ( NewsItem *newsItem in newsItems) {
if ( ![newsItem hasRead] )
return YES;
}
return NO;
}



kainjow
Apr 14, 2008, 10:06 PM
I don't think there's anything wrong with it. I've been using it in my code for years.

BTW fast enumeration is really just a do-while loop in disguise :)

lee1210
Apr 14, 2008, 10:14 PM
http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_7_section_1.html

You didn't state what newsItems is. If it's an NSArray, you should have no problem. If it's something you wrote that implements NSFastEnumeration, as long as you aren't storing any internal state that would be inconsistent and isn't initialized at the beginning of an enumeriation it should be fine.

-Lee

Spike099
Apr 14, 2008, 10:21 PM
Yeah it's an NSArray. Alright, the code stays. Thanks guys.

EDIT. Just looking at that documentation page, Apple even uses break many times.