Reputation: 40156
Is it redundant to check isKindOfClass even for non id variables in Objective C?
Teammate wrote Swift code. I am reviewing. Do I really need to check with isKindOfClass
in the condition or its redundant?
-(void) checkCalorie:(NSMutableDictionary *) update {
NSString *foodInfoId = [update objectForKey:FOOD_INFO_ID];
double calorie = [[update objectForKey:CALORIE] doubleValue];
if ([foodInfoId isKindOfClass:[NSString class]] && calorie < 0)
{
calorie = 0.0;
// some logic
}
}
Upvotes: 0
Views: 103
Reputation: 4521
I think you should refactor this code with the dynamic_cast
implemented for Objective-C. You can check this question for inspiration. For instance, use objc_dynamic_cast
from this answer, and then you will have the following code:
-(void)checkCalorie:(NSMutableDictionary *)update {
NSString *foodInfoId = objc_dynamic_cast([update objectForKey:FOOD_INFO_ID], NSString);
double calorie = [[update objectForKey:CALORIE] doubleValue];
if (foodInfoId != nil && calorie < 0) {
calorie = 0.0;
// some logic
}
}
Upvotes: 1