?
Solved

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

Posted on 2008-11-17
7
Medium Priority
?
625 Views
Last Modified: 2013-11-25
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
0
Comment
Question by:DJ_AM_Juicebox
  • 4
  • 3
7 Comments
 
LVL 24

Expert Comment

by:fridom
ID: 22982497
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
 

Author Comment

by:DJ_AM_Juicebox
ID: 22982607
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
 
LVL 24

Accepted Solution

by:
fridom earned 2000 total points
ID: 22983020
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:DJ_AM_Juicebox
ID: 22984616
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
 
LVL 24

Expert Comment

by:fridom
ID: 22984654
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
 

Author Comment

by:DJ_AM_Juicebox
ID: 22984697
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
 
LVL 24

Expert Comment

by:fridom
ID: 22985096
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question