In Swift 3, how do I fix the error about argument labels that don't match any available overloads for type String?

In Swift 2, I can load data from somefile.txtas the code below without a 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
    print("dataString=\(dataString)")
} catch let error as NSError {
    print("Failed reading data in appData Directory, Error: \(error.localizedDescription)")
}

However, in Swift 3, Xcode throws an error in the line dataString = try String(contentsOfURL: fileDestinationUrl)saying:

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

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

+4
source share
1 answer

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

dataString = try String(contentsOf: fileDestinationUrl) 

In Swift 3, all function parameters are now labeled, unless otherwise specified. This in practice often means that the last part of the method name moves to the first params label.

+2
source

Source: https://habr.com/ru/post/1656520/


All Articles