Need an array to store unnamed objects

Discussion in 'iOS Programming' started by adildacoolset, Jul 13, 2013.

  1. adildacoolset macrumors 65816

    Joined:
    Sep 5, 2011
    Location:
    Lusaka, Zambia(If you know where it is)
    #1
    Hello,

    I am having a bit of a dilemma. I require of an array to store 52 different objects, each with their own properties. Now, I am unable to declare 52 different objects, and name them all.

    A little help will be appreciated. Thanks!
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Declaration in C:
    Code:
    YourClassName * arrayName[ 52 ];
    
    Store an object into the array:
    Code:
    arrayName[ someIndex ] = [[YourClassName alloc] init];
    
    Refer to an indexed object:
    Code:
    someThing = arrayName[ someIndex ];
    
    If you really meant an NSArray of objects, or NSMutableArray of objects, then make an instance of the array class in the usual way, then fill it in the usual way, and access its members in the usual way.

    If neither of the above answers your question, then you need to explain your question more clearly.
     
  3. adildacoolset thread starter macrumors 65816

    Joined:
    Sep 5, 2011
    Location:
    Lusaka, Zambia(If you know where it is)
    #3
    I was in a bit of a rush, so sorry for not clarifying my question.

    I'm using an NSMutableArray, so here's my code:

    Code:
    
    //Declaration:
    NSMutableArray *arrayOfObjects = [[NSMutableArray alloc]init];
    
    //Assigning:
    
    for (int i = 0; i < 52; i++) {
            
            [self.arrayOfObjects addObject:[[MyClass alloc]init]];
            
     }
    
    //Accessing:
    [[self.arrayOfObjects objectAtIndex:index]property] = ... //Code to set the property to
    
    
    So that's what I tried, but I'm getting 2 errors:
    1. Multiple methods named "property" found wit mismatched result, parameter type, or attributes

    2. Assigning to 'readonly' return result of Objective-C not allowed.

    I can try your C method suggestion, but please also clarify what I'm doing wrong in the method above. Thank you :eek:

    Thank you very much for your time. And sorry for not clarifying well enough.
     
  4. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    That is definitely not how property setters are used in Obj-C.

    You have two possible syntaxes to choose from:

    Code:
    obj.property = value;
    or

    Code:
    [obj setProperty:value];
    Where obj would have [self.arrayOfObjects objectAtIndex:index] substituted in your case, regardless of which syntax you choose to go with.
     
  5. adildacoolset thread starter macrumors 65816

    Joined:
    Sep 5, 2011
    Location:
    Lusaka, Zambia(If you know where it is)
    #5
    Dang. I knew I made a noob mistake somewhere in the getters and setters– thank you for your time!
     
  6. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #6

    Actually ARC doesn't support memory management of C arrays that contain objects. If you use the syntax you proposed it will complain, and those objects would be deallocated as soon as the local variable that was used to create them went away.

    You'd have to use a __bridge_retained cast as you stored the objects in the C array, and then a __bridge_transfer when you extracted them from the C array.

    With ARC, memory management of C data structures (like C arrays) that contain NSObjects becomes a pain.

    Much better to use NSArrays, since they take ownership of the objects and release them when you remove them from the array.

    I will use C arrays of pointers to malloc'ed data sometimes, and then the memory management is totally up to me.
     
  7. adildacoolset thread starter macrumors 65816

    Joined:
    Sep 5, 2011
    Location:
    Lusaka, Zambia(If you know where it is)
    #7
    Thank you very much. I'll just use an NSArray to make things simpler.
     

Share This Page