views:

240

answers:

2

I need an NSDictionary which has key in the form of string (@"key1", @"key2") and value in the form of a C-style two-dimensional array (valueArray1,valueArray2) where valueArray1 is defined as :

int valueArray1[8][3] = { {25,10,65},{50,30,75},{60,45,80},{75,60,10},
                          {10,70,80},{90,30,80},{20,15,90},{20,20,15} };

And same for valueArray2.

My aim is given an NSString i need to fetch the corresponding two-dimensional array. I guess using an NSArray, instead of c-style array, will work but then i cannot initialize the arrays as done above (i have many such arrays). If, however, that is doable please let me know how.

Currently the following is giving a warning "Passing argument 1 of 'dictionaryWithObjectsAndKeys:' from incompatible pointer type" :

NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:valueArray1,@"key1",
                    valueArray2,@"key2",nil];

A: 

Rather than Adding Array Directly as a value in NSDictionary make a custom class in which create variable of NSArray ... and set this class object as value like

NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:MyClassObj1,@"key1",
                    MyClassObj2,@"key2",nil];

where MyClassObj1 and MyClassObj2 are member of MyClass

mihirpmehta
+2  A: 

Is valueArray2 also an int[][3]? If so, you could use

[NSValue valueWithPointer:valueArray1]

to convert the array into an ObjC value. To retrieve the content, you need to use

void* valuePtr = [[myDict objectForKey:@"key1"] pointerValue];
int(*valueArr)[3] = valuePtr;
// use valueArr as valueArrayX.

If there's just 2 keys, it is more efficient to use a function like

int(*getMyValueArr(NSString* key))[3] {
    if ([key isEqualToString:@"key1"]) return valueArray1;
    else return valueArray2;
}
KennyTM