Joe Huang Joe Huang - 23 days ago 15x
Swift Question

In Swift 3, how to fix an error about argument labels do not match any available overloads for String type?

In Swift 2, I could load data from

as the code below without problem:

let fileManager = FileManager.default
let urls = fileManager.urls(for: .applicationSupportDirectory, in: .userDomainMask)
let appDataURL = urls.last.appendingPathComponent("appData")

let fileDestinationUrl = appDataURL!.appendingPathComponent("somefile.txt")

var dataString = ""
do {
dataString = try String(contentsOfURL: fileDestinationUrl) //<-- error here
} catch let error as NSError {
print("Failed reading data in appData Directory, Error: \(error.localizedDescription)")

However, in Swift 3, XCode gives an error at line
dataString = try String(contentsOfURL: fileDestinationUrl)
by saying:

Argument labels '(contentsOfURL:)' do not match any available overloads

How to fix this error? What is the right way to read the text file in Swift 3?


This method has been updated to (in the context of your example):

dataString = try String(contentsOf: fileDestinationUrl) 

In Swift 3, all function params now have labels unless specifically defined otherwise. This in practice often means the last part of a method name moves to the first params label.