Question

I am working on converting a custom Objective-C Networking manager that I wrote for a client into Swift, as a way to learn the language and familiarize myself with the docs.

NSData *postData = [NSJSONSerialization dataWithJSONObject:userData options:0 error:&error];

[request setHTTPBody:postData];

However, when I open up the apple docs regarding setHTTPBody, it says it is no longer supported past 7.1?

In Swift, I have

/*Other Code*/

var request = NSMutableURLRequest(URL: sampleUrl) 
request.addValue("Content-Type", forHTTPHeaderField: "application/json")

I would assume a function along the lines of

request.setHTTPBody("data")

However - this does not seem to be the case... am I missing something? Or is there a new function to take the place of this deprecated one?

Was it helpful?

Solution

In Swift, a few of the classes doesn't have getters and setters methods anymore because we can set them to execute certain methods when the users set or get properties without using the setters and getters.

So in this example you can set the HTTPBody by directly accessing it:

request.HTTPBody = postData
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top