Categories question:
I’d like to place a category on NSURL that, among doing other things, overrides the query method to be able to provide a query string from a URL that is not RFC 1808 compliant. Is it legal to do something like this in my category:
- (NSString *)query //real NSURL method
{
if (stringIsRFC1808) //want to get the default implementation
return [super query];
else
return somethingElse; //want to get my custom implementation
}
thanks
The code as-is is not valid, because you’re using
[super query]– that doesn’t quite do what you expect it to do so. Categories aren’t superclasses. Here[super query]will try to invoke- [NSObject query]– BOOM an unrecognized selector error. If you’re using categories to extend a class, you won’t be able to call the original method – if you don’t need this functionality, then using categories is fine, if you do need it, however, you should consider subclassing, (even better composition for Foundation objets!) or using the Objective-C runtime to perform method swizzling – this way you’ll have access to the original implementation of the method.