Opening file from main bundle in Xcode 16

I've just upgraded to Xcode 16 and my app now fails when attempting to read a text file from the main bundle. It finds the path but can't open the file so this snippet prints: "File read error for path: /private/var/containers/Bundle/Application/AABAD428-96BC-4B34-80B4-97FA80B77E58/Test.app/csvfile.csv"

This worked fine up to 15.4. Has something changed? Thanks.

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    
    let bundle = Bundle.main
    
    let path = bundle.path(forResource: "csvfile", ofType: "csv")!
    
    do {
        let content = try String(contentsOfFile: path, encoding: String.Encoding.ascii)

    } catch {
        print("File read error for path: \(String(describing: path))")
    }
}

}

Answered by andsoff in 808975022

Problem solved. In case anyone else has something similar, it looks like Xcode 16 has become more strict in verifying ascii files. The same ascii import has been running on the same text file in my app for years, but now it seems that if there's even a single non-ascii character it gives the "Can't open the file" error. Previously it would just strip out any non-ascii characters and continue to load the file.

I got chatgpt to write me a clever little macro that checks for non-ascii characters in excel and highlights the cells they're in. Turns out there was an eclipse, a smart quote and a few invisible NBSP's which had been copied in from another source many years ago. Replacing those allowed the file to be read again.

Hi @andsoff , mmm, I will suggest, first at all , to catch the exception, for example in this way

do {
        let content = try String(contentsOfFile: path, encoding: String.Encoding.ascii)

    } catch let error {
        print("File read error : \(String(describing: error.localizedDescription))")
    }

Then, I'm no sure that you could decode a csv file as a string. Anyway, should be better working when read file with Data buffer, then I suggest to use something like this

let content = try Data(contentsOf: URL(fileURLWithPath: path))
print("File read success: \(content)")

If success, content should print read bytes.

Bye Rob

Hi Rob, Thanks for your help. The error description is:

"The file couldn’t be opened because it isn’t in the correct format".

But the Data(ContentsOf) does seem to read the file and prints the total bytes.

It's a csv file, but it just uses a | instead of a comma as its delimiter, as there are commas in the text in the fields. I've been doing it this way for several years and had no problem with Xcode opening the files until my upgrade of MacOS and Xcode yesterday.

I've just tried compiling a couple of the previous versions of the app and those now all fail with the same error at the same point too, so I think there must be a change in the way MacOS or Xcode validates csv files?

The goal of this is simply to read the csv rows into an array: csvArray = content.components(separatedBy: "\n") as [String]

and then iterate through each row and split into fields: let csvFields = thisCsvRow.components(separatedBy: "|")

Any ideas how else I could do this?

Thanks. Andrew.

Accepted Answer

Problem solved. In case anyone else has something similar, it looks like Xcode 16 has become more strict in verifying ascii files. The same ascii import has been running on the same text file in my app for years, but now it seems that if there's even a single non-ascii character it gives the "Can't open the file" error. Previously it would just strip out any non-ascii characters and continue to load the file.

I got chatgpt to write me a clever little macro that checks for non-ascii characters in excel and highlights the cells they're in. Turns out there was an eclipse, a smart quote and a few invisible NBSP's which had been copied in from another source many years ago. Replacing those allowed the file to be read again.

Hi @andsoff , great to hear that you resolved the problem. Anyway, I'm not sure could be the best way read csv as string with ascii decoding .

Try to check for example this project, we use many times to decode csv files https://github.com/yaslab/CSV.swift/tree/main

Bye Rob

it looks like Xcode 16 has become more strict in verifying ascii files

Ah, that rings a bell. See this thread.

Replacing those allowed the file to be read again.

Alternatively, you could supply the correct text encoding to String(contentsOfFile:encoding:) and these characters will come through intact. It seems weird to go out of your way to strip information like this.

Try to check for example this project, we use many times to decode csv files

Have you looked at TabularData framework? It’ll let you read and write CSV without taking any external dependencies. See TabularData Resources for links to docs and so on.

Share and Enjoy

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

Opening file from main bundle in Xcode 16
 
 
Q