这很可能是一个无效的URL。状态代码表示
NSFileReadUnknownError
,这根本没有用。您可能可以通过使用不同的URL初始化器来修复它。
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
let docURL = URL(fileURLWithPath: documentsDirectory) // <-- This line
let dataPath = docURL.appendingPathComponent("myDir").appendingPathComponent(fileName)
do {
let content = try String(contentsOf: dataPath, encoding: .utf8)
print(content)
} catch {
print("Failed to read file: \(error.localizedDescription)")
}