views:

60

answers:

2
+1  Q: 

NSArray vs array

is this OK practice?

NSArray* myarray[3] = {nil,nil,nil}

...

myarray[0] = some NSArray

or is it better to stick with NSArray over all?

NSArray* myarray = [NSArray...] ?
...
[myarray addObject:..]
+2  A: 

Depends on your needs. Either way works, but I generally find that mixing Objective-C objects and C structures leads to madness as you have to maintain sanity over two different memory models; retain/release and malloc/free.

I also generally avoid multi-dimensional arrays entirely. For example, a 3x4 array of arrays can be represented as a single array of 12 items. An item at 2,3 is really at (2 + (3*width)).

Note that NSArrays can't have "holes". You can represent the holes, if needed, with NSNull objects. Or create a subclass of NS[Mutable]Array that allows holes.

bbum
Curious why you avoid multi-dimensional arrays, @bbum. Not arguing for them but you're someone whose answers I respect and I'd like more insight as to why.
yabada
Multi-dimensional arrays imply multiple allocations and, assuming no holes, those multiple arrays really don't buy much in terms of implementation simplicity.
bbum
A: 

I will go for the second one, because in my opinion, the NSArray already encapsulates some advanced technique to help you.

It depends on your needs, if you want to do something complex and need built in data structure and algorithm, I think you can go for NSArray.

I try to avoid mixing between 2 programming languages C and obj-C as much as possible.

vodkhang