0

I am porting an app from Android Java to iPhone.

In Android I used Lists/ArrayLists alot.

On iPhone I plan to use NSMutableArray.

Is there any way to define or even indicate the type of objects in an NSMutableArray.

I know one can put any type of object there, but I would like to make it more visible and transparent.

Many thanks

user387184
  • 10,953
  • 12
  • 77
  • 147

3 Answers3

3

It's not clear exactly what you're asking.

If you just want to make it clear to the reader what sorts of object of are in the array, just name it appropriately (you can't enforce it at the language level):

NSMutableArray *arrayOfMyClasses;

If, on the other hand, you want to find out the type of an object that you're reading back from the array then you can get the underlying class using:

[obj class]

Or easily compare to other class types:

if ([obj isKindOfClass:[MyClass class]) { ... }

Tim

tarmes
  • 15,366
  • 10
  • 53
  • 87
2

I assume you are looking for template pattern in Objective C. Unfortunately, it is not available in Objective C (at least directly).

You might find this question of StackOverflow.com interesting

Community
  • 1
  • 1
Sarwar Erfan
  • 18,034
  • 5
  • 46
  • 57
1

You can only indicate a type.

for(id obj in _assets) {
    NSString *className = NSStringFromClass([obj class]);
    NSLog(@"%@", className);
}

Arrays are ordered collections of any sort of object. For example, the objects contained by the array in Figure 1 can be any combination of cat and dog objects, and if the array is mutable you can add more dog objects. The collection does not have to be homogeneous.

Collections Programming Topics - Arrays: Ordered Collections

beryllium
  • 29,669
  • 15
  • 106
  • 125