I’m attempting to learn Swift. I’m writing and testing a Class to encapsulate variables and methods to process a binary file of UInt16 values to produce and display a GCImage in an app for testing the Class which displays the grayscale image as the ultimate test.
At this point most everything worked and I was tidying up the code. Suddenly importing the file using Data produced a runtime error. The file could not be read.
Consulting Dr. Google was a waste of time. In the meantime I compared the file path input as a String vs the generated URL. The only difference was that spaces had been replaced by ‘%20’. I finally created some debug code to test that the data file actually existed (it’s in my file tree) and was readable – it is. I have inspected the entitlements file and the app is not Sandboxed and read permission is ‘Yes’. (In the process I somehow damaged the entitlements file, couldn’t recreate it so had to create a new app and copy all the code.) The error persists.
XCode and Swift are up-to-date, the app is only for macOS which is up-to-date.
The paths below are copied from debug data
imagePath = (String) "/Users/nate/2023 02 22 1330 SwiftDecomTestData/rawPlus/DN/IR3_2017-12-08_018_029_dn.raw"
fileURL Foundation.URL? "/Users/nate/2023%2002%2022%201330%20SwiftDecomTestData/rawPlus/DN/IR3_2017-12-08_018_029_dn.raw"
The code snippet where the error occurs.
private func importAndScaleImageData() {
// Start DEBUG verification
let filePath = self.imagePath // data file
if checkIfFileExists(atPath: filePath) {
print("file exists!")
if !checkFileReadability(filePath: filePath) {
print("Unable to read the file.")
print("imagePath (filePath)")
fatalError()
}
} else {
print("file does not exist!")
fatalError()
}
// No runtime errors above #########
// END DEBUG verification
do { // NOTE input file path as String was cast as URL
let data8 = try Data(contentsOf: self.fileURL!) // ERROR
self.backingImage = data8.uint16Array() // not reached
} catch { // error caught here #######
print("importAndScaleImageData: URL (self.fileURL!) (error)")
fatalError("Error found importing (self.fileURL!.absoluteString)")
}
// remainder of the method skipped
}