0

I wanted to subclass URLSessionConfiguration to set some default values for properties such as timeout intervals and httpHeaders. So I came with this custom subclass.

class CustomSessionConfiguration: URLSessionConfiguration {
    init(time: TimeInterval) {
        super.init()
        timeoutIntervalForResource = time
    }    
}

But when I instantiated CustomSessionConfiguration a crash occurred with the following reason:

-[CustomSessionConfiguration setTimeoutIntervalForResource:]: unrecognized selector sent to instance 0x6000021b76c0

It seems that my subclass didn't inherit the setter method for timeoutIntervalForResource. Note that the same happens for other properties of the class such as httpAdditionalHeaders.

francybiga
  • 960
  • 1
  • 8
  • 16

1 Answers1

3

After more carefully inspecting the documentation for URLSessionConfiguration I noticed that init() is marked as deprecated. This is a clear signal that the class is not intended to be subclassed as I was doing. In particular the call to super.init() in my subclass' init was probably creating a "broken" instance.

Moreover, if I create an instance using the default class method:

let config = URLSessionConfiguration.default
print(type(of: config))

I get the value __NSCFURLSessionConfiguration which indicates pretty clearly that NSURLSessionConfiguration is likely implemented as a Class Cluster and this is probably the reason why CustomSessionConfiguration wasn't inheriting property accessors.

francybiga
  • 960
  • 1
  • 8
  • 16