Structs with arrays

Discussion in 'iOS Programming' started by jgbradley1, Jun 18, 2009.

  1. jgbradley1 macrumors newbie

    Joined:
    Jun 18, 2009
    Location:
    Morehead, KY
    #1
    Ok, so I've been reading the Kochan Objective-C book and I have a question. I'm right now developing an app where I need to utilize the use of a struct with several NSMutable arrays. The examples in the Kochan book use struct examples that involve the int, double, and struct (adding a struct within a struct). I was wondering about the process you would go about putting a NSMutableArray in a struct. If you look at the sample below, I am wondering if I need to declare NSMutableArray *dataList in the struct or would I do something like Code #2 below. Now I know you have to list the data type, so if I did something like code #2, what data type would I use for dataList? Would it be NSMutableArray since dataList is a pointer to a NSMutableArray? I have been reading more on pointers, so if I've used the wrong terminology or said something wrong, please point it out in a "constructive criticism" sort of way. Thank you so much in advance for the help. I have one last general question. I hope this makes sense. If I am declaring an array to be used as a pointer in the "instance variable" section of the interface for example, would I declare, allocate, and initialize it all at the same time in the interface section of the header file or elsewhere? If somewhere else, could you give me any pointers/tips on what your programming experience has taught you?

    CODE #1:

    struct data{
    int data1;
    double data2;
    NSMutableArray *dataList;
    };



    CODE #2:

    NSMutableArray *dataList = [[NSMutableArray alloc] initWithCapacity:0];

    struct data{
    int data1;
    double data2;
    NSMutableArray dataList;
    }
     
  2. skochan macrumors regular

    Joined:
    Apr 1, 2006
    Location:
    California
    #2
    Hi,

    You may want to continue this question in the forum for the book (www.classroomM.com/objective-c). I'll try to answer your questions, although I'm not sure I fully understand them. Others can chime in.

    In the case of #1, you would obviously have to initialize the structure. I don't know where or how you intend to use it. Initializing the dataList member of the structure could be done with a sequence like this:

    Code:
    struct data foo;
    ..
    foo.data1 = 100;
    foo.data2 = 200.0;
    foo.dataList = [[NSMutableArray alloc] initWithCapacity:0];
    ...
    In the second case, you need a * in front of dataList in the struct definition as it's a pointer. I would also not use the same name for the array and the structure member. A sequence like this would then work:

    Code:
    NSMutableArray *dataList = [[NSMutableArray alloc] initWithCapacity:0];
    
    struct data{
       int data1;
       double data2;
       NSMutableArray *dataArray;
    };
    
    struct data foo;
    ...
    foo.data1 = 100;
    foo.data2 = 200.0;
    foo.dataArray = dataList;
    ...

    For the last part of your question, there are examples in the text (see the AddressBook examples in Part II) that illustrate how to work with arrays as instance variables. The technique shown there is to allocate the array with an init method.

    A couple of questions: Do you need to mix C structs with Foundation arrays like this? Could this all be handled as instance variables in a class?


    Cheers,

    Steve Kochan
     

Share This Page