Swift `CocoaError.Code.fileNoSuchFile` has unexpected raw value

I am running Swift 6.3 on WSL2 in Windows 11. The following behavior confuses me:

do {
    let data = try Data(contentsOf: fileThatDoesNotExist)
} catch CocoaError.fileNoSuchFile {
    // This is not hit since `fileNoSuchFile.rawValue == 4`
    print(CocoaError.fileNoSuchFile)
} catch let error as CocoaError {
    // This is hit and `error.code.rawValue == 260`
    print(error)
} catch {
    // N/A
}

Is this a known issue on Linux, or am I just doing something wrong? What is the cross-platform way to catch Cocoa errors?

260 is fileReadNoSuchFile. Unfortunately none of the constants in CocoaError.Code have any documentation so I can't say with any degree of certainty which one is "expected"...

Yeah 260 as fileReadNoSuchFile seems expected to me, fileReadNoSuchFile is typically used for non-existent files when reading while fileNoSuchFile is typically used when writing to something expected to exist but does not.

@bbrk24 @jmschonfeld fileReadNoSuchFile is the error I was looking for. Thank you both for your help!

1 Like