An Array of id's

Discussion in 'Mac Programming' started by Jordan72, Nov 27, 2005.

  1. macrumors member

    Joined:
    Nov 23, 2005
    #1
    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.
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    You want to create a dynamic array, or static?

    Code:
    id myArray[10];
    should work fine.... (I think)
     
  3. macrumors 6502a

    MacCoaster

    Joined:
    Jul 15, 2002
    Location:
    Washington, DC / Rochester, NY / Lexington, NC
    #3
    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];
     
  4. thread starter macrumors member

    Joined:
    Nov 23, 2005
    #4
    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
     
  5. macrumors 6502a

    MacCoaster

    Joined:
    Jul 15, 2002
    Location:
    Washington, DC / Rochester, NY / Lexington, NC
    #5
    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. :)
     
  6. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    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).
     
  7. macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #7
    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.
     
  8. thread starter macrumors member

    Joined:
    Nov 23, 2005
    #8
    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.
     
  9. macrumors 6502a

    MacCoaster

    Joined:
    Jul 15, 2002
    Location:
    Washington, DC / Rochester, NY / Lexington, NC
    #9
    In the days of GHz G4s, G5s and Yonahs along with cheap RAM, does it really matter that much?
     
  10. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #10
    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.
     
  11. macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #11
    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.
     

Share This Page