Skip to content

Commit 5318433

Browse files
committed
Swift 3 support for URLSession
1 parent d3adc82 commit 5318433

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

src/targets/swift/nsurlsession.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,17 @@ module.exports = function (source, options) {
4747
// we make it easier for the user to edit it according to his or her needs after pasting.
4848
// The user can just add/remove lines adding/removing body parameters.
4949
code.blank()
50-
.push('var postData = NSMutableData(data: "%s=%s".dataUsingEncoding(NSUTF8StringEncoding)!)', source.postData.params[0].name, source.postData.params[0].value)
50+
.push('let postData = NSMutableData(data: "%s=%s".data(using: String.Encoding.utf8)!)', source.postData.params[0].name, source.postData.params[0].value)
5151
for (var i = 1, len = source.postData.params.length; i < len; i++) {
52-
code.push('postData.appendData("&%s=%s".dataUsingEncoding(NSUTF8StringEncoding)!)', source.postData.params[i].name, source.postData.params[i].value)
52+
code.push('postData.append("&%s=%s".data(using: String.Encoding.utf8)!)', source.postData.params[i].name, source.postData.params[i].value)
5353
}
5454
break
5555

5656
case 'application/json':
5757
if (source.postData.jsonObj) {
58-
code.push(helpers.literalDeclaration('parameters', source.postData.jsonObj, opts))
58+
code.push(helpers.literalDeclaration('parameters', source.postData.jsonObj, opts),'as [String : Any]')
5959
.blank()
60-
.push('let postData = NSJSONSerialization.dataWithJSONObject(parameters, options: nil, error: nil)')
60+
.push('let postData = JSONSerialization.data(withJSONObject: parameters, options: [])')
6161
}
6262
break
6363

@@ -79,13 +79,13 @@ module.exports = function (source, options) {
7979
.push(1, 'body += "Content-Disposition:form-data; name=\\"\\(paramName)\\""')
8080
.push(1, 'if let filename = param["fileName"] {')
8181
.push(2, 'let contentType = param["content-type"]!')
82-
.push(2, 'let fileContent = String(contentsOfFile: filename, encoding: NSUTF8StringEncoding, error: &error)')
82+
.push(2, 'let fileContent = String(contentsOfFile: filename, encoding: String.Encoding.utf8)')
8383
.push(2, 'if (error != nil) {')
84-
.push(3, 'println(error)')
84+
.push(3, 'print(error)')
8585
.push(2, '}')
8686
.push(2, 'body += "; filename=\\"\\(filename)\\"\\r\\n"')
8787
.push(2, 'body += "Content-Type: \\(contentType)\\r\\n\\r\\n"')
88-
.push(2, 'body += fileContent!')
88+
.push(2, 'body += fileContent')
8989
.push(1, '} else if let paramValue = param["value"] {')
9090
.push(2, 'body += "\\r\\n\\r\\n\\(paramValue)"')
9191
.push(1, '}')
@@ -94,35 +94,35 @@ module.exports = function (source, options) {
9494

9595
default:
9696
code.blank()
97-
.push('let postData = NSData(data: "%s".dataUsingEncoding(NSUTF8StringEncoding)!)', source.postData.text)
97+
.push('let postData = NSData(data: "%s".data(using: String.Encoding.utf8)!)', source.postData.text)
9898
}
9999
}
100100

101101
code.blank()
102102
// NSURLRequestUseProtocolCachePolicy is the default policy, let's just always set it to avoid confusion.
103-
.push('var request = NSMutableURLRequest(URL: NSURL(string: "%s")!,', source.fullUrl)
104-
.push(' cachePolicy: .UseProtocolCachePolicy,')
103+
.push('let request = NSMutableURLRequest(url: NSURL(string: "%s")! as URL,', source.fullUrl)
104+
.push(' cachePolicy: .useProtocolCachePolicy,')
105105
.push(' timeoutInterval: %s)', parseInt(opts.timeout, 10).toFixed(1))
106-
.push('request.HTTPMethod = "%s"', source.method)
106+
.push('request.httpMethod = "%s"', source.method)
107107

108108
if (req.hasHeaders) {
109109
code.push('request.allHTTPHeaderFields = headers')
110110
}
111111

112112
if (req.hasBody) {
113-
code.push('request.HTTPBody = postData')
113+
code.push('request.httpBody = postData as Data')
114114
}
115115

116116
code.blank()
117117
// Retrieving the shared session will be less verbose than creating a new one.
118-
.push('let session = NSURLSession.sharedSession()')
119-
.push('let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in')
118+
.push('let session = URLSession.shared')
119+
.push('let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in')
120120
.push(1, 'if (error != nil) {')
121-
.push(2, 'println(error)')
121+
.push(2, 'print(error)')
122122
.push(1, '} else {')
123123
// Casting the NSURLResponse to NSHTTPURLResponse so the user can see the status .
124-
.push(2, 'let httpResponse = response as? NSHTTPURLResponse')
125-
.push(2, 'println(httpResponse)')
124+
.push(2, 'let httpResponse = response as? HTTPURLResponse')
125+
.push(2, 'print(httpResponse)')
126126
.push(1, '}')
127127
.push('})')
128128
.blank()

0 commit comments

Comments
 (0)