Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Jordan72

macrumors member
Original poster
Nov 23, 2005
88
0
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

Moderator emeritus
Jun 15, 2000
7,958
7
You want to create a dynamic array, or static?

Code:
id myArray[10];
should work fine.... (I think)
 
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.

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

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

Jordan72

macrumors member
Original poster
Nov 23, 2005
88
0
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
 
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:

Code:
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

Moderator emeritus
Jun 15, 2000
7,958
7
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

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
id is defined as something like
Code:
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

macrumors member
Original poster
Nov 23, 2005
88
0
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.
 

caveman_uk

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
Jordan72 said:
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

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
caveman_uk said:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.