• C

Using a plain array? (do they exist in obj-c)?


I want to make a fixed size array of objects - can we do this or do you have to use this nsArray contraption? In java it would just be:

    Car[] mycars = new Car[100];

Who is Participating?
fridomConnect With a Mentor CEO/ProgrammerCommented:
I see,  you'd better use  the  following then

NSArray * cars  = [NSArray initWithCapacity:  200 * 100];
filling the stuff  with
[cars insertObject: [[Cars alloc] init]  atIndex: 1];

You have to think a bit different in Objective-C. But if you insist you can do the following

Cars * cars = malloc(100 * 200 * sizeof(*Cars));
and then you can access the elements as usually in C

Car at_1 = cars[1];

However I'd to  the Objective C way... But keep in  mind you can always fall back  to C

Well you can  always use "plain" array as you can in C, Objective-C is really just a small extension to Standard C so you can write

NSString arr[100] and you have an Array of  NSStrings ;-)

I  still would vote against it.  If you have "classes" I'd stick to them.

DJ_AM_JuiceboxAuthor Commented:
Ok I see - I need to allocate a 200 x 100 array of objects, so I thought it would be more efficient to allocate them like:

    Car* cars = new Car[200 * 100];

Rather than creating an NSArray of Car and adding each instance one at a time. Should I not even worry about it? I'm developing for iPhone so I'm not sure what the best route is.

Actually I only got NSMutableArray to work, by adding elements one at a time. Plain NSArray would work but I don't see how to do something like:

    NSArray* p = new NSArray(200 * 100);
    P[15] = new Car();

Looks like you had to specify all elements in it's constrictor call?

Please excuse the c++ syntax, just started obj c and am quite uncomfortable with it's syntax still!

Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

DJ_AM_JuiceboxAuthor Commented:
Whoa you're allowed to use plain old C memory management with Objective-C?
In  any case, the NSArray example you gave was what I was looking for, thanks.
As written  before Objective C is a superset of C. And so yes you can hamper around with malloc/free, any other C toolkit like e.g libapr. so  in  fact you can base Objective C classes on some C structures and just wrap them a bit. This  is a very elegant solution and it works really fine

DJ_AM_JuiceboxAuthor Commented:
Hmm one thing though, NSArray claims (in xcode)  that initWithCapacity is not a method! Does iPhone use some subset or different version of NSArray that does not have that method?
Please check the docs from NSArray, I have looked up in the GNUStep documentation. I could not know that you
are talking about Macs implementation. Howerver you can use things like arrayWithObjects or you can use
NSMutableArray, that's also more what you'd expect from C arrays.

There is a function initWithCapacity, which probably is what you are looking for

NSMutableArray *arr = [NSMutableArray initWithCapacity: 200 * 100];

But keep in mind the elements in this array can be whatever there is you could have an Array where you can store cars beside houses, NSStrings, NSNumbers, and whatever you like. So any container in Objective C is a generic container.  I'd suggest strongly not  to mix it all  too wild because one may forgot what one has  store there.

C++ is much more restrictive here and you must use templates for the same generality Objective C provides without extra "work".


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.