NSDictionary 上的快速枚举失败,并显示“[Waypoint countByEnumerateWithState:objects:count:]: 无法识别的选择器发送到实例...”

发布于 2024-08-18 05:03:50 字数 2595 浏览 10 评论 0原文

我的数据位于 NSDictionary 对象中,其中键是转换为 NSValues 的 CGPoints,对象是 UIColors。这是我用来从字典返回对象的方法:

- (UIColor*) getTemperatureColor2 {
    NSDictionary* temperatureColorMap = [Weather getTemperatureColorMap];   

    for(id key in temperatureColorMap) {
        CGPoint point = [key CGPointValue];
        if ( (int)roundf(self.temperature_celsius) >= (int)roundf(point.x)  ) { 
            if ( (int) roundf(self.temperature_celsius) <= (int) roundf(point.y) ) {
                return [temperatureColorMap objectForKey:key];
            }
        }       
    }

    return [UIColor blackColor];    
}

这是 getTemperatureColorMap 方法,在同一类(天气)中实现:

+ (NSDictionary*) getTemperatureColorMap {
    static NSDictionary* temperatureColorMap = nil;

    if (temperatureColorMap == nil) {
        temperatureColorMap = [[[NSDictionary alloc] initWithObjectsAndKeys:
                            RGB2UIColor(0x0E09EE), [NSValue valueWithCGPoint: CGPointMake(-99, -8)],
                            RGB2UIColor(0xB85FC), [NSValue valueWithCGPoint:  CGPointMake(-7, -3) ],
                            RGB2UIColor(0x0BDCFC), [NSValue valueWithCGPoint: CGPointMake(-2, 2) ],
                            RGB2UIColor(0x1BBA17), [NSValue valueWithCGPoint: CGPointMake(3, 7) ],
                            RGB2UIColor(0x45F90C), [NSValue valueWithCGPoint: CGPointMake(8, 12) ],
                            RGB2UIColor(0xF9F60C), [NSValue valueWithCGPoint: CGPointMake(13, 17) ],
                            RGB2UIColor(0xF9B20C), [NSValue valueWithCGPoint: CGPointMake(18, 22) ],
                            RGB2UIColor(0xF9780C), [NSValue valueWithCGPoint: CGPointMake(23, 27) ],
                            RGB2UIColor(0xFE3809), [NSValue valueWithCGPoint: CGPointMake(28, 32) ],
                            RGB2UIColor(0xFE0909), [NSValue valueWithCGPoint: CGPointMake(33, 99) ], nil] autorelease];
    }

    return temperatureColorMap;
}

我在 for 循环中调用 getTemperatureColor2 (遍历所有路径点),这全部在绘制矩形方法。航路点包含天气对象。

routeAnnotation.lineColor = [fromWaypoint.weather getTemperatureColor2];

当视图加载时,drawRect 方法被调用两次(我需要这个来达到效果)。第一次一切都很好,但第二次当代码到达快速枚举 for 循环时,我得到一个异常:

2010-01-15 11:40:42.224 AppName[1601:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[Waypoint countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x856d170'

现在我不知道 Waypoint 中的错误是如何发生的,因为它是我正在迭代的 NSDictionary。另外,我完全不明白为什么需要再次调用 drawRect 才会导致迭代失败!

I have my data in a NSDictionary object where the keys are CGPoints converted to NSValues and the objects are UIColors. Here's the method I'm using to return an object from the dictionary:

- (UIColor*) getTemperatureColor2 {
    NSDictionary* temperatureColorMap = [Weather getTemperatureColorMap];   

    for(id key in temperatureColorMap) {
        CGPoint point = [key CGPointValue];
        if ( (int)roundf(self.temperature_celsius) >= (int)roundf(point.x)  ) { 
            if ( (int) roundf(self.temperature_celsius) <= (int) roundf(point.y) ) {
                return [temperatureColorMap objectForKey:key];
            }
        }       
    }

    return [UIColor blackColor];    
}

This is the getTemperatureColorMap method, implemented in this same class (Weather):

+ (NSDictionary*) getTemperatureColorMap {
    static NSDictionary* temperatureColorMap = nil;

    if (temperatureColorMap == nil) {
        temperatureColorMap = [[[NSDictionary alloc] initWithObjectsAndKeys:
                            RGB2UIColor(0x0E09EE), [NSValue valueWithCGPoint: CGPointMake(-99, -8)],
                            RGB2UIColor(0xB85FC), [NSValue valueWithCGPoint:  CGPointMake(-7, -3) ],
                            RGB2UIColor(0x0BDCFC), [NSValue valueWithCGPoint: CGPointMake(-2, 2) ],
                            RGB2UIColor(0x1BBA17), [NSValue valueWithCGPoint: CGPointMake(3, 7) ],
                            RGB2UIColor(0x45F90C), [NSValue valueWithCGPoint: CGPointMake(8, 12) ],
                            RGB2UIColor(0xF9F60C), [NSValue valueWithCGPoint: CGPointMake(13, 17) ],
                            RGB2UIColor(0xF9B20C), [NSValue valueWithCGPoint: CGPointMake(18, 22) ],
                            RGB2UIColor(0xF9780C), [NSValue valueWithCGPoint: CGPointMake(23, 27) ],
                            RGB2UIColor(0xFE3809), [NSValue valueWithCGPoint: CGPointMake(28, 32) ],
                            RGB2UIColor(0xFE0909), [NSValue valueWithCGPoint: CGPointMake(33, 99) ], nil] autorelease];
    }

    return temperatureColorMap;
}

I call getTemperatureColor2 in a for loop (going through all the waypoints), which is all in the drawRect method. A waypoint contains a weather object.

routeAnnotation.lineColor = [fromWaypoint.weather getTemperatureColor2];

When the view loads, the drawRect method is called twice (I need this for an effect). The first time everything is fine, but the second time as soon as the code reaches the fast enumeration for loop I get an exception:

2010-01-15 11:40:42.224 AppName[1601:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[Waypoint countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x856d170'

Now I have no idea how the error is in Waypoint as it's a NSDictionary that I'm iterating through. Also, I absolutely don't understand why it takes another call to drawRect for the iteration to fail!

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

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

发布评论

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

评论(2

甜妞爱困 2024-08-25 05:03:50

您想要对字典中的键执行快速枚举,如下所示:

for(NSValue *key in [temperatureColorMap allKeys])

UPDATE
尽管我的建议使意图更加清晰,但这绝对不是您所看到的异常原因(我现在意识到 NSDictionary 实现了快速枚举,并且它必须位于键数组上)。

我现在认为这可能是一个内存错误,因为您正在自动释放字典(但对它的静态引用在释放时不会设置为 nil),但即使多次运行您的方法,我也无法重现该异常。

我的代码和你的代码之间的唯一区别是我将对 RGB2UIColor 的调用更改为对 Objective-C 方法的调用。
您没有提供它的实现,但我可以假设它能够返回正确的 Objective-C UIColor 对象吗?

You want to perform fast enumeration on the keys in the dictionary like this:

for(NSValue *key in [temperatureColorMap allKeys])

UPDATE
Although my suggestion makes the intent more clear, it is definitely not the cause of exception that you are seeing (I realize now that NSDictionary implements fast enumeration and it must be on the array of keys).

I am now thinking it might be a memory error since you are autoreleasing the dictionary (but the static reference to it doesn't get set to nil when it's released), but I can not reproduce the exception even running your method many times.

The only difference between my code and yours is that I changed the call to RGB2UIColor into a call to an objective-C method instead.
You didn't provide its implementation, but can I assume it is able to return a proper objective-C UIColor object?

神经大条 2024-08-25 05:03:50

我认为快速枚举的默认数组语法会自动适用于 NSDictionary

for(MyClass* instance in dictionary){  // <- this works for NSArray
    // process instance here
}

但是,这似乎会产生两个对象(的实例) MyClass)字典中的键(NSString的实例)。由于在 NSString 上调用 MyClass 方法,我的应用程序崩溃了。所以我最终这样做了:

for(MyClass* instance in [dictionary allValues]){ // (as opposed to 'allKeys')
    // process instance here
}

I thought the default array syntax for fast enumeration would automagically work for NSDictionary:

for(MyClass* instance in dictionary){  // <- this works for NSArray
    // process instance here
}

However, this seems to yield both objects (instances of MyClass) and keys from the dictionary (instances of NSString). My app was crashing because of MyClass methods being called on NSString. So I ended up doing this:

for(MyClass* instance in [dictionary allValues]){ // (as opposed to 'allKeys')
    // process instance here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文