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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With