2

Yesterday I upgraded XCode and used Apple's translator to change the code from Swift 2 to Swift 3.

Of course there were errors, most of them easy to fix, but there is one that is killing me.

I am making a NSMutableURLRequest. In swift 2, to set the body of the request I did this:

let body = NSMutableData()
//append data
request.httpMethod = "POST"
request.httpBody = body

When I used the translator, all it added was a as Data:

request.httpBody = body as Data

Here it shows the first error:

Cannot convert value of type NSMutableData to type Data in coercion

After trying hours to parse both NSMutableData and NSData to Data and failing (I don't know why it isn't possible) I decided to change the body and the way to append data to type Data. I thought this was going to end the problems, but I was wrong. Now, when I set the httpBody it shows the following error:

Cannot convert value of type Data? to type Data?enter image description here I thought this could be a bug but I've restarted my computer and reinstalled XCode and same error.

Is this a bug or am I doing something wrong? Is there an easy way to parse NSMutableData to Data that I didn't find?

Thanks!

  • Strange, your first approach `let body = NSMutableData(); req.httpBody = body as Data` compiles for me. – How is `body` defined in your second approach `req.httpBody = body`? – Can you provide a small *self-contained* example demonstrating the problem? – Martin R Oct 10 '16 at 19:03

0 Answers0