Bar *obj = [[Bar alloc] init];
// ...
for (id elem in obj) {
...
}
requires that the class Bar
conforms to the NSFastEnumeration
Protocol, i.e. it must implement the
countByEnumeratingWithState:objects:count:
method. (This is the case for all Objective-C collection classes such asNSArray
, NSDictionary
, NSSet
.)
So the direct answer to your question is no, you cannot use the fast enumeration syntax for (... in ...)
to enumerate all properties of an arbitrary class.
However, it is possible to implement the fast enumeration protocol for a custom class. Examples how this is done can be found here