1

Possible Duplicate:
Is there any way to enforce typing on NSArray, NSMutableArray, etc.?

I'm Java programmer and I'm starting on Obj-C, in java i can create a mutable array with determined type of class, like as follow:

ArrayList<MyClass> list;

in Obj-c I know the NSMutableArray but i don't know and not found how to determinate the type of class into it.

Have a way of make this with it or other class without NSMutableArray which can do this?

Thanks a lot.

Community
  • 1
  • 1
ademar111190
  • 14,215
  • 14
  • 85
  • 114
  • The questions you are asking is not clear. Could you elaborate more? – SayeedHussain Oct 26 '12 at 19:42
  • OP wants a parameterized array in Objectve C. – Marko Topolnik Oct 26 '12 at 19:43
  • @crypticcoder ok, I'will do it. – ademar111190 Oct 26 '12 at 19:43
  • That'd be *mutable* array, not *multable*. – Caleb Oct 26 '12 at 19:46
  • 1
    this thread might be helpful: http://stackoverflow.com/questions/8190530/does-objective-c-support-generics Note, that I posted 2 answers with the second one showing an approach, where the array can perform test prior to adding. – vikingosegundo Oct 26 '12 at 19:48
  • @vikingosegundo very cool, I'll test this, but probably i not go use in my project because i wanna use a native class. – ademar111190 Oct 26 '12 at 19:52
  • I am not using it either in real world :) as I wrote in the first answer: if there is a need to limit the array to a certain type, most likely your architecture is buggy. – vikingosegundo Oct 26 '12 at 19:54
  • @vikingosegundo Good decision :), I have not yet tested it but seems like a good code, but it is better to adapt to the style of language to try to make it seem like the java. – ademar111190 Oct 26 '12 at 19:57
  • possible duplicate of [Is there any way to enforce typing on NSArray, NSMutableArray, etc.?](http://stackoverflow.com/q/649483/), [NSMutableArray - force the array to hold specific object type only](http://stackoverflow.com/q/5197446/) – jscs Oct 26 '12 at 19:57
  • Objective-C does not support parameterized types directly. However you can code something close yourself, see [NSMutableArray - force the array to hold specific object type only](http://stackoverflow.com/questions/5197446/nsmutablearray-force-the-array-to-hold-specific-object-type-only/5198040#5198040). – CRD Oct 26 '12 at 19:48

2 Answers2

4

No, Cocoa/Objective-C doesn't offer typed collections like this. All objects in the collection must inherit from NSObject (which is basically everything besides primitives and structs), but beyond that, it's up to you to understand/manage what is going on in the array. Objects in an NSMutableArray are represented in its interface by the generic type id.


From a design standpoint, collections in Cocoa typically do contain homogeneously-typed objects. The name of the array is often used to indicate what's inside it (just as in Java), e.g. bookTitlesArray or just bookTitles (i.e. strings). Additionally, from an abstraction standpoint, sometimes lightweight classes are used to "wrap" a raw NSMutableArray to enforce type checking at the interface. As in, a new class called BookTitleList which offered a subset of the add, lookup, remove methods and passed them through to an internal array after e.g. validation. But YMMV depending on your needs.

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
  • Understood, it seems necessary to understand a little better the architecture of Objectiv-c, will find a way to do it by the standards of language but I appreciate everyone's attention! – ademar111190 Oct 26 '12 at 20:00
0

Although there are no type parameters in Objective C, you'll find it much less of a nuisance than in Java because you don't have to downcast to call a method. Objective C is more like JavaScript in this respect.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436