9

I'm in the process of migrating one of my projects to Swift 3 and I'm hung up on converting a NSURLRequest to NSURLMutableRequest. In Swift 2 I could simply:

let mreq = req.mutableCopy() as! NSMutableURLRequest

But now mutableCopy is no longer a thing in Swift 3. I tried various permutations of constructors and looked in the docs for info to no avail. I must be missing something. There has to be a way to make a mutable copy of an object.

tidwall
  • 6,881
  • 2
  • 36
  • 47

1 Answers1

17

I just figured it out. Dang it was too obvious.

let mreq = req.mutableCopy() as! NSMutableURLRequest

becomes

var mreq = req
Max MacLeod
  • 26,115
  • 13
  • 104
  • 132
tidwall
  • 6,881
  • 2
  • 36
  • 47
  • 10
    Just use URLRequest and declare it as var. – Leo Dabus Jul 02 '16 at 21:34
  • How do you mean? Could you provide an example? It was already a URLRequest but came from an event that passed it in as an immutable variable through 'let'. I need to make a copy of the variable and add some headers. The only way I could figure it out was to make a mutable copy was to assign it to an variable and declare it as a var. – tidwall Jul 03 '16 at 05:02
  • If it came as let you can just make it variable `var req = req` – Leo Dabus Jul 03 '16 at 06:08
  • Note that I am talking about Swift 3 URLRequest not NSURLRequest. – Leo Dabus Jul 03 '16 at 06:13
  • Makes sense. Thanks for your help. – tidwall Jul 03 '16 at 10:45