Runtime error when trying to read a file

Hello, I'm a newer Xcode developer trying to debug an error I'm getting in my iOS app. I'm using this code to get file content:

//inputFile = "here is my file name with no extension" let filepath = Bundle.main.path(forResource: inputFile, ofType: "txt") //filepath = "/Users/username/Library/Developer/CoreSimulator/Devices/[DeviceGUID]/data/Containers/Bundle/Application/[AppGUID]/AppName.app/here is my file name with no extension.txt"

let fileContent = try String(contentsOfFile: filepath)

That line generates a runtime error: error NSError domain: "NSCocoaErrorDomain" - code: 264 0x0000600000c74d20

Xcode 16.2 on macOS Sequoia 15.1.1

I had this code working... I had to step away from the code for a few months; I updated Xcode and now I'm getting this error. (I could've screwed the code up). I navigated to the directory and the file is there.

Anybody have any ideas?

Thank you! Hoss

Answered by DTS Engineer in 826660022

Error 264 is NSFileReadUnknownStringEncodingError, which suggests you have a text encoding issue. The most common text encoding on Apple platforms is UTF-8. If you change the code to:

let fileContent = try String(contentsOfFile: filepath, encoding: .utf8)

what happens?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

Error 264 is NSFileReadUnknownStringEncodingError, which suggests you have a text encoding issue. The most common text encoding on Apple platforms is UTF-8. If you change the code to:

let fileContent = try String(contentsOfFile: filepath, encoding: .utf8)

what happens?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Runtime error when trying to read a file
 
 
Q