Can I do a fast enumeration directly via a property?
if I have a property
@pr开发者_如何学Gooperty (nonatomic, retain) NSArray* myArray;
Can I do ? And if yes why does this work ?
for (id object in self.myArray)
;
Or do I need to do ?
NSArray* r = self.myArray;
for (id object in r)
;
It works because self.myArray is syntactical sugar for [self myArray], which is generated by the @synthesize keyword. So really you're doing:
for (id object in [self myArray])
And the return value of [self myArray] implements the fast enumeration protocol so the for..in syntax can work on it.
Does that make things clearer?
Yes you can use fast enumeration like that. To answer the question from your comments, I believe that the fast enumeration protocol will throw an exception if you modify your array property during enumeration.
http://www.mikeash.com/pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html
You can definitely go for for(id object in self.array)
精彩评论