0

I'm subclassing NSURLRequest. Creating custom init methods for the class created. While coding, i ended up with the following error.. "'required' initializer 'init(coder:)' must be provided by subclass of 'NSURLRequest'"...

My code is

import Foundation

class URLRequest: NSURLRequest {
    init (UrlRequestWithURL: NSString, setHTTPBody: NSData, shouldHandleCookies: Bool, isHTTPMethodPost: Bool, withTimeoutInterval: NSTimeInterval)
    {

    }

// Here it shows the error
}

Help me regarding this..

Apart from the above question....

  1. Do i really need to subclass NSUrlRequest.. (Purpose of creating urlrequest variable, is that creating several requests that have to assigned for urlsession variable.) Else is there any other way that the purpose gets satisfied?

  2. How to create a singleton class in swift. All these days i worked with objc. I find hard in formulating it.

  • Re #2: Everything about Swift "singletons" is said in http://stackoverflow.com/questions/24024549/using-a-dispatch-once-singleton-model-in-swift. – Martin R Jul 20 '16 at 05:25
  • Since NSURLRequest implements `NSCoding` you must also implement `NSCoding`, which requires that you implement the required initialiser. Do you need to subclass NSURLRequest? Probably not, but you haven't clearly explained why you are doing so. There is a good chance you can use an extension – Paulw11 Jul 20 '16 at 05:30
  • add init(coder decoder: NSCoder) { super.init(coder: decoder) } and try – Anbu.Karthik Jul 20 '16 at 05:31

1 Answers1

0

I would avoid subclassing NSURLRequest. I've encountered very strange behavior when I do so (and progressively worse misbehavior the farther back you support iOS-version-wise).

You're better off adding category methods and using NSURLProtocol's propertyForKey:inRequest: and setProperty:forKey:inRequest: methods.

dgatwood
  • 10,129
  • 1
  • 28
  • 49