在 NSDictionary 中存储/提取对象

发布于 2024-12-09 15:49:00 字数 963 浏览 0 评论 0原文

当尝试将 NSDictionary 的内容分配给新变量/对象时,我收到错误。也许这不可能?我以为会是这样。我也不确定是否可以在字典中使用非 Objective C 特定对象。你可以吗?

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

这是我调用来创建字典的方法:

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

//do stuff to image

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

return testImage_andNumChars;
}

这不是处理这个问题的正确方法吗?创建字典时出现的错误是:

“无法在参数传递中将‘IplImage*’转换为‘objc_object*’”

,当我检索字典元素时得到:

“无法将‘objc_object*’转换为‘IplImage*’初始化”和“从‘objc_object*’到‘int’的无效转换”。

我已经阅读了 NSDictionary 上的 Apple 文档,这让我走到了这一步,但我不知道从这里该去哪里。

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初吻给了烟 2024-12-16 15:49:00

简而言之,NSDictionary 值必须是 NSObject

您应该使用例如将int存储为NSNumber

[NSNumber numberWithInt:numChars]

当检索您可以使用的值时,例如:

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

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];
远昼 2024-12-16 15:49:00

IplImage 是严格的,numchars 是 int。由于两者都不是客观的 c 对象,因此您不能将它们按原样存储在字典中。您需要创建一个代表它们的对象。对于 numchars,您应该存储一个 NSNumber 对象。要取回该值,您可以对从 objectForKey 返回的 NSNumber 调用 intValue 。

对于 IplImage 来说有点复杂。您可以将其包装在自己的类中,也可以扩展 NSValue。

最主要的是字典只能存储objective c对象。

IplImage is a strict and numchars is an int. since neither one is an objective c object, you cannot store them as such in the dictionary. You need to create an object that represents them. In the case of numchars, you should store an NSNumber object. To get the value back you would call intValue on the NSNumber you get back from objectForKey.

For the IplImage it is a little more complicated. You could wrap it in a class of its own, or you could extend NSValue.

The main thing is that The dictionary can only store objective c objects.

兮子 2024-12-16 15:49:00

此外,您不能将整数添加到字典中 - 它必须是一个对象。因此,将您的整数包装在 NSNumber 中,应该没问题。

Also you cannot add an integer to a dictionary - it needs to be an object. So wrap your integer in a NSNumber and you should be ok.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文