Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Cocoa have a @protocol for iterable / collection objects?

Tags:

objective-c

I'm writing an API, and I'm a firm believer of putting as much type safety as possible into APIs. Is there a @protocol for things like NSSet, NSArray, etc that marks it as "iterable" in a for (foo in bar) {..} style loop? At the moment I've got something like this:

- (void) doSomethingWith:(id)someItems;

When I'd like to have something along these lines:

- (void) doSomethingWith:(id <NSIterableCollection>)someItems;

Is it doable? Does wanting it make me some kind of static-typing weanie who has no place doing Obj-C? ;-)

like image 817
Sophistifunk Avatar asked Dec 08 '22 02:12

Sophistifunk


2 Answers

Objects that want to support enumeration using the standard Objective-C fast enumeration (for..in construct), must impelement the NSFastEnumeration. NSArray, NSDictionary, NSSet and NSEnumerator adopt this protocol.

like image 97
Franci Penov Avatar answered Jan 05 '23 00:01

Franci Penov


Yes. NSFastEnumeration.

like image 45
jtbandes Avatar answered Jan 05 '23 00:01

jtbandes