Search code examples
iphoneobjective-copencvnsdictionaryiplimage

Storing/Extracting Objects to/from NSDictionary


I am getting errors using a NSDictionary when trying to assign it's contents to new variables/objects. Maybe this isn't posible? I thought it would be. I'm also not sure if I can use non-Objective C specific objects in the dictionary. Can you?

NSDictionary *preProcessResult = [[NSDictionary alloc] initWithDictionary: [self preProcessing:testImage]];
IplImage* resultImage = [preProcessResult objectForKey:@"ResultImage"];
int numChars = [preProcessResult objectForKey:@"NumberChars"];
[preProcessResult release];

And here is the method I am calling to create the dictionary:

- (NSDictionary*) preProcessing: (IplImage*) testImage {

//do stuff to image

NSDictionary *testImage_andNumChars = [NSDictionary dictionaryWithObjectsAndKeys:resultImage, 
@"ResultImage", numChars, @"NumberChars", nil];

return testImage_andNumChars;
}

Is this not the correct way to handle this? The error I get when I create the dictionary is:

"Cannot convert 'IplImage*' to 'objc_object*' in argument passing"

and when I retrieve the dictionary elements I get:

"Cannot convert 'objc_object*' to 'IplImage*' in initialization" and "Invalid conversion from 'objc_object*' to 'int' ".

I've read the Apple docs on NSDictionary which got me this far, but I am not sure where to go from here.

Thanks in advance.


Solution

  • Putting it shortly, NSDictionary values must be NSObjects.

    You should store int as NSNumber using e.g.:

    [NSNumber numberWithInt:numChars]
    

    When retrieving the value you can use e.g.:

    int numChars = [[preProcessResult objectForKey:@"NumberChars"] intValue];