• C

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

Hi,

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];

thanks
DJ_AM_JuiceboxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

fridomCEO/ProgrammerCommented:
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.

Regards
Friedrich
0
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!

0
fridomCEO/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
stuff.

Regards
Friedrich
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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.
0
fridomCEO/ProgrammerCommented:
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

Regards
Friedrich
0
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?
0
fridomCEO/ProgrammerCommented:
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".

Regards
Friedrich



0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.