0

I have 2 arrays that were created dynamically. I have read some examples on the net, and I could not understand it fully. What I see, is that the arrays seems to be 1 Dimension and not 2.

The codes below assigns some objects to array "combineObjectIssues", which will then be added into "combineAll" to get a 2D-Array. I want "currentObject.date" to be index 0, while "issuesDiscovered" array to be index 1.

for (currentObject in currentObjects) {
        [combineObjectIssues addObject:currentObject.date]; //2D Array Row
        for (Issue *checkIssue in currentObject.issuesDiscovered) {
            if (checkIssue) {
                [issuesDiscovered addObject:checkIssue];
            }
        }
        [tempIssues addObject:[issuesDiscovered copy]]; // to combine all array of issues
        [combineOjectIssues addObjectsFromArray:[issuesDiscovered copy]]; //2D Array column
        [combineAll addObject:[combineObjectIssues copy]];
        [issuesDiscovered removeAllObjects]; //remove all objects;
        [combineObjectIssues removeAllObjects]; //remove all objects
    }
}

Below is my output for combineAll array.

(
    (
    "2013-07-19 09:00:00",
    "<Issue: 0x8c171f0>",
    "<Issue: 0x8c16e50>",
    "<Issue: 0x8c16d30>",
    "<Issue: 0x8c16a10>",
    "<Issue: 0x8c16090>",
    "<Issue: 0x8c15bb0>",
    "<Issue: 0x8c156d0>"
),
    (
    "2013-07-13 14:30:00"
),
    (
    "2013-06-08 14:30:00",
    "<Issue: 0x8c10340>",
    "<Issue: 0x8c0fad0>",
    "<Issue: 0x8c0f590>",
    "<Issue: 0x8c0f0c0>"
),
    (
    "2013-05-04 11:30:00"
)
)

As you can see from the output, its a 1 dimension array, which I do not want. I want to have something like, for [0][0], it contains the date, whereby for [0][1], it contains an array of issues.

I know my codes might not be right. As such, kindly assist me. Your assistance(s) are appreciated.

3 Answers 3

2

Try this:

you can always get like that values but syntax can be different.

[[combineAll objectAtIndex:0] objectAtIndex:1];

this way you can get the value for [0][1]. you can do like this to make it 2 dimensional array.

also you can do this:

NSString* str = combineAll[0][1];

For all the issue data you can do this:

NSArray* issueArray = combineAll[0];

this will return all the issues at position 0 of combineAll array.

Hope it Helps!!

4
  • 1
    You can use the literal syntax for readability: combineAll[0][1]; is equivalent. Aug 22, 2013 at 9:35
  • I did that. But it only grab <Issue: 0x8c171f0> instead of the array of issues which contains 7 issues for [0][1]. Aug 22, 2013 at 9:41
  • it will return one value only because at 0 position there is array with 8 objects in it. now you have called [0][1] that means it will return object at number 1 from that 8 value array. Aug 22, 2013 at 9:45
  • How can I call the whole array of issues dynamically at row 0 for example? This is important as I need to set the section based on the date, and the number of rows based on the array that is associated to a particular dat. Aug 22, 2013 at 9:50
1

The question of multidimensional data structures to store Cocoa objects comes up frequently. Sometimes it can be solved by re-thinking your object hierarchy in a way that better fits the conceptual model. But sometimes a multidimensional array is the best mechanism. Ordinary C arrays can store pointers to Cocoa objects, and may be a better way of dealing with this. By way of illustration:

#import <Foundation/Foundation.h>

@interface Foo : NSObject
@property (nonatomic,strong) NSString *name;
@end

@implementation Foo
@end

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        id p[10][10];
        //  create a bunch of foos
        for( uint8_t i = 0; i < 10; i++ ) {
            for( uint8_t j = 0; j < 10 ; j++ ) {
                Foo *aFoo = [[Foo alloc] init];
                aFoo.name = [NSString stringWithFormat:@"Foo_%02d_%02d",i,j];
                p[i][j] = aFoo;
            }
        }
        //  show that we can recover a Foo from C array
        Foo *someFoo = (Foo *)p[5][5];
        NSLog(@"Foo[5][5] = %@",someFoo.name);
        // prints Foo[5][5] = Foo_05_05 to the console
    }
    return 0;
}

Or, if you want to allocate the C array dynamically, you'll have little more work to do. See this gist

0

You're going to make life hard on yourself by using this kind of data structure. You'll always have to remember that the first index is a different type than the rest of them, and always having to do index shifting and so forth. But if you must, try something like this:

NSMutableArray *output = [NSMutableArray array];
for (YourClass *currentObject in currentObjects) {
    //Making assumption currentObject.issuesDiscovered in an NSArray! if it's not you'll need to initialize the second element differently
    NSArray *arrayForObject = @[currentObject.date,[currentObject.issuesDiscovered copy]];
    [output addObject:arrayForObject];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.