Search code examples
iosobjective-carraysdynamic-arrays

Creating & accessing Arrays dynamically in objective-c


I'm developing an ios app and need to be able to somehow create arrays during runtime, add them to the mainDataArray and access them later on to be able to add new objects to them if I need to. Is that even possible or do I need to create 100 array properties and initialize them 1 by 1 when I need them? And if it works how could it be done?

Problem solved


Solution

  • Yes this is possible depends on you need but you can create array on runtime ( mutable or imutable ).

    Here's a snippet:

    //init
    NSMutableArray *arr = [NSMutableArray array];
    
    
    //on demend
    NSMutableArray *newArr = [NSMutableArray array];
    [newArr addObject:myObject];
    [arr addObject:newArr];
    
    
    //pulling my object
    
    Object *obj = [[arr objectAtIdex:0] objectAtIndex:0];