Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

swift

swift3

In Swift 2, I could load data from somefile.txt 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
    print("dataString=\(dataString)")
} 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?

like image 247
Joe Huang Avatar asked Oct 30 '22 19:10

Joe Huang


1 Answers

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.

like image 104
syllabix Avatar answered Nov 15 '22 05:11

syllabix