Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 27, 2005, 10:45 PM   #1
Jordan72
macrumors member
 
Join Date: Nov 2005
An Array of id's

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.
Jordan72 is offline   0 Reply With Quote
Old Nov 28, 2005, 12:04 AM   #2
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
You want to create a dynamic array, or static?

Code:
id myArray[10];
should work fine.... (I think)
kainjow is offline   0 Reply With Quote
Old Nov 28, 2005, 07:55 AM   #3
MacCoaster
macrumors 6502a
 
MacCoaster's Avatar
 
Join Date: Jul 2002
Location: Washington, DC / Rochester, NY / Lexington, NC
Send a message via AIM to MacCoaster
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];
__________________
The Mac Coaster
Quad Core Q6600 PC, 23" Apple Cinema HD LCD, White MacBook C2D 2.16 GHz, Athlon64 PC, AthlonXP PC, iPod photo 60 GB
MacCoaster is offline   0 Reply With Quote
Old Nov 28, 2005, 03:32 PM   #4
Jordan72
Thread Starter
macrumors member
 
Join Date: Nov 2005
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
Jordan72 is offline   0 Reply With Quote
Old Nov 28, 2005, 09:42 PM   #5
MacCoaster
macrumors 6502a
 
MacCoaster's Avatar
 
Join Date: Jul 2002
Location: Washington, DC / Rochester, NY / Lexington, NC
Send a message via AIM to MacCoaster
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.
__________________
The Mac Coaster
Quad Core Q6600 PC, 23" Apple Cinema HD LCD, White MacBook C2D 2.16 GHz, Athlon64 PC, AthlonXP PC, iPod photo 60 GB
MacCoaster is offline   0 Reply With Quote
Old Nov 28, 2005, 10:11 PM   #6
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
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).
kainjow is offline   0 Reply With Quote
Old Nov 28, 2005, 11:09 PM   #7
Catfish_Man
macrumors 68030
 
Catfish_Man's Avatar
 
Join Date: Sep 2001
Location: Portland, OR
Send a message via AIM to Catfish_Man
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.
Catfish_Man is offline   0 Reply With Quote
Old Nov 29, 2005, 11:33 PM   #8
Jordan72
Thread Starter
macrumors member
 
Join Date: Nov 2005
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.
Jordan72 is offline   0 Reply With Quote
Old Nov 30, 2005, 07:40 AM   #9
MacCoaster
macrumors 6502a
 
MacCoaster's Avatar
 
Join Date: Jul 2002
Location: Washington, DC / Rochester, NY / Lexington, NC
Send a message via AIM to MacCoaster
In the days of GHz G4s, G5s and Yonahs along with cheap RAM, does it really matter that much?
__________________
The Mac Coaster
Quad Core Q6600 PC, 23" Apple Cinema HD LCD, White MacBook C2D 2.16 GHz, Athlon64 PC, AthlonXP PC, iPod photo 60 GB
MacCoaster is offline   0 Reply With Quote
Old Nov 30, 2005, 07:59 AM   #10
caveman_uk
Guest
 
caveman_uk's Avatar
 
Join Date: Feb 2003
Location: Hitchin, Herts, UK
Quote:
Originally Posted by Jordan72
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.
caveman_uk is offline   0 Reply With Quote
Old Dec 8, 2005, 12:47 AM   #11
Catfish_Man
macrumors 68030
 
Catfish_Man's Avatar
 
Join Date: Sep 2001
Location: Portland, OR
Send a message via AIM to Catfish_Man
Quote:
Originally Posted by caveman_uk
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.
Catfish_Man is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Access an array of objects stored in one view controller from a different vc isthisonetaken Mac Programming 4 Mar 23, 2011 08:07 PM
I want an array of ALL the Objects in a Window BadWolf13 Mac Programming 48 Dec 21, 2010 05:09 PM
An Array Of Dictionaries Not Writing To File Correctly? Darkroom iPhone/iPad Programming 6 Aug 29, 2009 10:22 AM
convert an array of ints - into a string with semicolon in between. celia Mac Programming 20 Jun 26, 2007 03:41 PM
Mini Mac has an array of features MacBytes MacBytes.com News Discussion 0 Jan 24, 2005 01:23 AM


All times are GMT -5. The time now is 04:51 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC