PDA

View Full Version : An Array of id's




Jordan72
Nov 27, 2005, 10:45 PM
Using straight Objective-C, I want to create an array of id's. Anyone know how to do this?

Note: I am not try to rewrite the great Foundation classes we already have. I'm just trying to get a better understanding of using the language.



kainjow
Nov 28, 2005, 12:04 AM
You want to create a dynamic array, or static?

id myArray[10]; should work fine.... (I think)

MacCoaster
Nov 28, 2005, 07:55 AM
Yes, id myArray[ 10 ]; would work.

Jordan72, have a look at NSArray and NSMutableArray. They can hold any objects, even just id. NSArray is fixed length. NSMutableArray is dynamic.

id object2 = @"bleh";
NSArray * array = [NSArray arrayWithObjects:@"blah", object2, nil];

NSMutableArray * mutableArray = [NSMutableArray arrayWithObjects:@"blah", nil ];
[mutableArray addObject: object2];

Jordan72
Nov 28, 2005, 03:32 PM
I actually want to create a mutable array.

Since it's going to be mutable, I need a pointer to dynamically changing memory. That's where I'm having the trouble.

How should I declare my pointer to a memory location that will hold id types?

Like this?:

id *myArray;

And then allocate it dynamically like this?:

myArray = malloc(sizeof(arraySize)); //arraySize is count of id in array

MacCoaster
Nov 28, 2005, 09:42 PM
Honestly I don't see a reason to do that mess; I'm also not sure if your code would work. sizeof(arraySize)? Where is arraySize?

I think you mean the sizeof of the datatype, for example:

id myArray = (id*)malloc(p_size * sizeof(id));

Though that might be a bit difficult, I'm not sure if objective-C gives an explicit size for `id'.

NSMutableArray is your friend. It does the dirty work for you.

But seriously, if you want to do it without using Cocoa classes, you're looking at pure C. Objective-C is a subset of C, so just look up dynamic arrays for C and use it in your objective-C class. I think my included code above generally shows it, but I could be wrong. It's just that NSMutableArray makes our lives so much easier. :)

kainjow
Nov 28, 2005, 10:11 PM
There's not a lot of reason to do a mutable array structure from the ground up. If you're studying Computer Science or something related, you'll definitely learn how it works, but for anything else, it's not extremely helpful.

C++ already has dynamic arrays, but they can't hold multiple data types (as far as I know).

Catfish_Man
Nov 28, 2005, 11:09 PM
id is defined as something like
typedef id struct objc_class* (may not be exactly right, but it's close), so sizeof(id) == sizeof(int*) == etc...

But yeah, use NSMutableArray. It's your friend.

Jordan72
Nov 29, 2005, 11:33 PM
Your comments inspired me to do a p_size search and I ended up finding a really good artical on dynamic arrays for c.

http://www.geocities.com/fsairin/dynam-1.html

Isn't there some circumstances in object oriented programming where using dynamic c arrays is neccessary for efficiency, because objects require too much overhead?

If so, I think it's clever to learn these ideas involved with making a dynamic array.

MacCoaster
Nov 30, 2005, 07:40 AM
In the days of GHz G4s, G5s and Yonahs along with cheap RAM, does it really matter that much?

caveman_uk
Nov 30, 2005, 07:59 AM
Isn't there some circumstances in object oriented programming where using dynamic c arrays is neccessary for efficiency, because objects require too much overhead?

Depends where your performance bottleneck is. It's a more productive use of time to fix things that you know cause performance problems (by using tools such as Shark) rather than optimising something that works 'well enough'. Often a better designed algorithm will give much better returns.

Catfish_Man
Dec 8, 2005, 12:47 AM
Depends where your performance bottleneck is. It's a more productive use of time to fix things that you know cause performance problems (by using tools such as Shark) rather than optimising something that works 'well enough'. Often a better designed algorithm will give much better returns.

Indeed. You can have the most heavily tuned bubble-sort in the world and it'll still be slow as molasses compared to even a bad implementation of quicksort.