tags:

views:

62

answers:

4

Whilst learning Objective-C I ran into a situation where I needed to add two different objects (one inherits from the other) to an array. Whilst it does work I was not 100% sure it was good practice?

@interface TireBasic : NSObject {
}
@end

@interface TireSnow : TireBasic {
}
@end

// To this array?
TireBasic *tires[4];

cheers -gary-

+1  A: 

Looks good. Another option would be to use the id type.

Asaph
I did quickly try using the id type earlier but I got mixed up somewhere. Like I say I will do a smaller testBed program tomorrow to get it working in isolation.
fuzzygoat
+1  A: 

There's nothing wrong with that. Basic polymorphism at work here.

Philippe Leybaert
A: 

Arrays are not typed in Objective-C so you can have any type of object as an array element. As to whether it is good practice or not, it depends entirely on how your planning on using the array

ennuikiller
A: 

I fail to see why NSArray could not be used here. NSArray can contain multiple object types.

Jasconius
Hi Jasconius, the plainC array was just where I ended up with my little test. I will as you and a few other have suggested have a look at NSArray and id. Many thanks ...
fuzzygoat
An `NSArray` can often be unnecessary overhead especially when your arrays are as small as 4 elements. It does provide some nifty features for enumeration and value checking, but if those aren't required then a plain C array will often suffice.
dreamlax