views:

2718

answers:

4

What I really like in C# are generic lists. A list that can contain only one type of objects. Is there something like a generic list in Cocoa/Objective-C? As far I only know NSArray who will take a pointer to any object.

+4  A: 

Objective-C doesn't support generic programming. You could always use Objective-C++ and an STL list.

Ferruccio
+14  A: 

Wanting this is often a sign of a weak design. NSArray is immutable, so it will not "take a pointer to any object" and presumably already contains the correct objects when handed to you. What I assume you're more worried about is an NSMutableArray where you think other parts of your code might add the wrong sort of object. But have a look at Cocoa itself; it's incredibly rare to expose a mutable array as part of a class's design.

Instead, you generally expose an NSArray and a couple of methods for modifying that array. Something along the lines of:

@class Foo : NSObject
- (NSArray *)bars;
- (void)addBar:(Bar *)bar;
- (void)removeBar:(Bar *)bar;
@end

This generally stops wrong objects being inserted simply by having a compiler warning, and then of course you can add assertions within -addBar: and -removeBar: if you wish too.

Mike Abdullah
Great answer. Thanks a lot. Just what I had in mind.
Holli
Also, if you need more advanced operations, read up on -mutableArrayValueForKey:
Mike Abdullah
A: 

Why doesn't Objective-C support generic programming? Both NSNumber and NSString support compare:, so why couldn't one write (e.g.) a generic version of min or max?

Chris Wicklein
Generics aren't really necessary in a dynamically-typed language.
mipadi
This space is for answers to the question asked. If you want to ask a question of your own, you can do that, but the "Your Answer" box isn't the place to do it.
Chuck
A: 

My question is rhetorical and directed towards Ferruccio's statement that Objective-C doesn't support generic programming. More to the original question, there may not be a standard "NSList", but I don't see why one couldn't be implemented, and the Cocoa collections which do exist like NSArray can contain heterogeneous objects, so they might as well be generic in the sense of the STL (not type-safe, but generic.) Less relevant to the original question but more relevant to the issue of generic programming, I mean to say that because Objective-C classes appear to implement methods like compare:, there IS no reason one could not write free functions which operate on Objective-C class instances in much the same way that templated functions in the STL operate on C++ class instances. Objective-C may not be generic in exactly the same way that C++ is, but it could support a style of programming not entirely unlike generic programming ala the STL.

Christopher Wicklein
This is not an answer. This is a question.
Dave DeLong
Ah, I should probably be commenting and not answering .. still figuring out the interface. This post (http://ridiculousfish.com/blog/archives/2005/12/23/array/) explores performance characteristics of NSArray which suggest it isn't strictly implemented as an array and may be surprisingly (to someone use to STL vectors and lists) adequate in place of a linked list.
Christopher Wicklein
Ah, I missed the original requirement that the list be type restricted for the repeated mention of the list being generic. I think it's worth noting that a programming system can be generic in the sense Alexander Stepanov seems to mean without having the sort of type safety C++ provides.
Christopher Wicklein