Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setHTTPBody in iOS 8 + Swift

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?

like image 472
Michael Voznesensky Avatar asked Oct 20 '22 07:10

Michael Voznesensky


1 Answers

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
like image 138
Enrico Susatyo Avatar answered Oct 27 '22 23:10

Enrico Susatyo