枚举 NSMutableDictionary ——无法从循环内访问对象属性

发布于 2024-12-10 12:25:16 字数 1230 浏览 0 评论 0原文

我有一个 NSMutableDictionary,analyzedPxDictionary,其中包含一堆 Pixel 对象(我创建的自定义类)。除此之外,Pixel 对象还包含一个名为 rgb 的 NSArray 属性。该数组将始终包含三个 NSNumber 对象,其中整数值对应于像素的 RGB 值。

我现在尝试使用快速枚举来枚举 analyzedPxDictionary。但是,我似乎无法从循环内访问像素对象的属性。我已将 rgb 声明为合成属性,以便我可以使用点语法访问它。但是,当我尝试在循环内执行此操作时,程序崩溃,给出如下错误:

'-[NSCFString rgb]: unrecognized selector sent to instance 0xa90bb50'

这是一个示例产生该错误的代码:

for (Pixel *px in analyzedPxDictionary) {
    printf("r: %i, g: %i, b: %i",[[px.rgb objectAtIndex:0] integerValue], [[px.rgb objectAtIndex:1] integerValue], [[px.rgb objectAtIndex:2] integerValue]);
}

我尝试在该 printf 行上设置断点以检查 px。虽然 rgb 被列为一个(如果其属性并正确描述为 NSArray 的实例),但它似乎不包含任何对象。

我相信我正确初始化了 rgb 。为了进行解释,请考虑以下代码:

NSString *key;
for (Pixel *px in analyzedPxDictionary) {
    key = [px description];
}

Pixel *px = [analyzedPxDictionary objectForKey:key];
printf("\nr: %i, g: %i, b: %i",[[px.rgb objectAtIndex:0] integerValue], [[px.rgb objectAtIndex:1] integerValue], [[px.rgb objectAtIndex:2] integerValue]);

这成功地将正确的值打印到控制台。

那么为什么我无法从 forin 循环中访问 rgb 属性呢?

I have an NSMutableDictionary, analyzedPxDictionary, containing a bunch of Pixel objects (a custom class I created). Among other things, Pixel objects contain an NSArray property called rgb. That array will always contain three NSNumber objects, with integer values corresponding to the pixel's rgb values.

I'm now trying to enumerate over the analyzedPxDictionary using fast enumeration. However, It seems like I'm not able to access Pixel objects' properties from within the loop. I have declared rgb to be a synthesized property so that I can access it using dot syntax. But when I try to do so from within the loop, the program crashes, giving me an error like the following:

'-[NSCFString rgb]: unrecognized selector sent to instance 0xa90bb50'

Here's an example of the code that produces that error:

for (Pixel *px in analyzedPxDictionary) {
    printf("r: %i, g: %i, b: %i",[[px.rgb objectAtIndex:0] integerValue], [[px.rgb objectAtIndex:1] integerValue], [[px.rgb objectAtIndex:2] integerValue]);
}

I've tried setting a break point on that printf line in order to inspect px. While rgb is listed as one if its properties and correctly described as an instance of NSArray, it doesn't seem to contain any objects.

I believe that I'm initializing rgb correctly. To explain, consider the following code:

NSString *key;
for (Pixel *px in analyzedPxDictionary) {
    key = [px description];
}

Pixel *px = [analyzedPxDictionary objectForKey:key];
printf("\nr: %i, g: %i, b: %i",[[px.rgb objectAtIndex:0] integerValue], [[px.rgb objectAtIndex:1] integerValue], [[px.rgb objectAtIndex:2] integerValue]);

This successfully prints the correct values to the console.

So why can't I access the rgb property from within the forin loop?

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

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

发布评论

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

评论(1

々眼睛长脚气 2024-12-17 12:25:16

NSDictionaries 的快速枚举为您提供每个键,而不是每个值。所以你需要这样做:

for (NSString *key in analyzedPxDictionary) 
{
    Pixel *px = [analyzedPxDictionary objectForKey:key];
    printf("r: %i, g: %i, b: %i",[[px.rgb objectAtIndex:0] integerValue], [[px.rgb objectAtIndex:1] integerValue], [[px.rgb objectAtIndex:2] integerValue]);
}

Fast enumeration of NSDictionaries gives you each key, not each value. So you'd need to do:

for (NSString *key in analyzedPxDictionary) 
{
    Pixel *px = [analyzedPxDictionary objectForKey:key];
    printf("r: %i, g: %i, b: %i",[[px.rgb objectAtIndex:0] integerValue], [[px.rgb objectAtIndex:1] integerValue], [[px.rgb objectAtIndex:2] integerValue]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文