I have data in an NSDictionary object, where the keys are CGPoints converted to NSValues and the objects are UIColors. Here is the method I use to return an object from a 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 the 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 (passing through all the waypoints), which is all used in the drawRect method. Waypoint contains a weather object.
routeAnnotation.lineColor = [fromWaypoint.weather getTemperatureColor2];
When the view loads, the drawRect method is called twice (I need this for the effect). The first time, everything is fine, but the second time, as soon as the code reaches a quick enumeration for the 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'
, Waypoint, NSDictionary, . , , drawRect !