Dominique Charlebois Dominique Charlebois - 1 year ago 100
Swift Question

Swift 3 - Error - Cannot invoke 'data' with an argument list of type '(using: String.Encoding)'

I've got a mobile SDK that was working prior to the Swift 3 Migration.

I get the following error :

Cannot invoke 'data' with an argument list of type '(using: String.Encoding)'


open class func applyTheCode(
_ theCode: String,
forTenant tenant: String,
toUserID userID: String,
toAccountID accountID: String,
withToken token: String,
completionHandler: @escaping (_ userInfo: AnyObject?, _ error: NSError?) -> Void) {

let url = baseURL.appendingPathComponent("path/to/api/call")
let request = NSMutableURLRequest(url: url)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue(token, forHTTPHeaderField: "token")
request.httpMethod = "POST"
request.httpBody = NSString(string: "{}").data(using: String.Encoding.utf8)

let dataTask = companyDataTaskStatusOKWithRequest(request as URLRequest, withCallback: completionHandler)

The line in question:

request.httpBody = NSString(string: "{}").data(using: String.Encoding.utf8)

My question is is there a better way of writing this line of code, or has anyone ran into this problem and have been successful in migrating it into swift 3 syntax.

Things I've tried :

request.httpBody = String("{}").data(using: String.Encoding.utf8)

But I'm not sure it's the same, but I could be mistaken.

Any direction or links or answers welcomed :D

Answer Source

You are using NSString , what you should do is use String

request.httpBody = "{}".data(using: String.Encoding.utf8)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download