Is casting a bad thing? What are peoples thoughts on the matter? Should it be avoided where possible? or a necessary evil?
The reason I ask is that i'm following the Stanford lectures and working my way through the assignments and on the first one they ask you to make an NSDictionary and only print out the values from entries into the dictionary which have a key with a certain prefix.
Thats fine I thought as I wrote my first verision of the for loop:
Code:
for (NSDictionary *entry in bookmarkDict)
{
if ([entry hasPrefix:@"Stanford"])
{
NSLog(@"Key: '%@' URL: '%@'", entry, [bookmarkDict objectForKey:entry]);
}
}
Without using the Cast I get an warning: "warning: 'NSDictionary' may not respond to '-hasPrefix:'"
Whats the problem you may be thinking? A Simple cast and the warning dissappears! Well me being the perfectionist I would quite like my finished solution (and for future reference too) to have no cast, if possible.
So, have I missed a method name somewhere in the documentation or is this a case where a cast is the only solution?