0

I'm writing a subclass of NSURL (along with a partnering subclass of NSURLProtocol). I'm intentionally stashing an additional data property on the URL object intended to be retrieved and used in the URL loading. (This is just an experiment; don't judge.)

However, when I test out the class combo by creating one of these custom URL objects and then trying to load from it...

NSData * data = [NSData dataWithContentsOfURL:myURL];

...my URL protocol gets handed an actual NSURL instance to inspect, not the instance that I created. Its absoluteString is what I expect-- in other words, the loading internals have copied my original URL to a new one-- but it's not my custom subclass, so it's missing the extra property.

Note that my NSURL subclass correctly overrides -copyWithZone: for NSCopying compliance and both -initWithCoder: and -encodeWithCoder: for NSSecureCoding compliance. This should ensure that anyone making a shallow or deep copy of an instance of my class should get a full, correct copy. But none of these methods are called during this process. (Neither is absoluteString, path, relativeString, etc.)

Someone's "copying" out my URL's (string) contents without calling any of the methods I'd expect to see. I suppose another Foundation class could just be bypassing the public interface and accessing a private/internal property to do this.

Can anyone confirm if NSURL is fundamentally un-subclassable? Or if I might be missing something?

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
  • A guess would be that something in the toll-free bridge to CFURL fails to call into Objective-C for some reason but I can't see anything obvious in http://opensource.apple.com/source/CF/CF-855.17/CFURL.c – Tommy Jan 26 '15 at 00:57
  • 1
    It would help if you showed more relevant code. – rmaddy Jan 26 '15 at 01:09
  • This is long past usefulness for me, but to future travelers: there isn't really any relevant code-- question is about whether there's a way to subclass `NSURL` and see that subclass instance appear in the URLProtocol object. – Ben Zotto Jun 22 '17 at 18:52

0 Answers0