3

I am a swift beginner and I am trying to write a function that reads the contents of a *.csv file and return a 2-D array [[Double]].

I am using Xcode 10.1 running on macOS 10.14.

I am using the following code (in a playground) to read the contents of the file into a String:

let home = FileManager.default.homeDirectoryForCurrentUser // 

let file = "Documents⁩/MyAppName⁩/x.csv"

let url = home.appendingPathComponent(file)

url.path // This gives: "/Users/userName/Documents⁩/MyAppName⁩/x.csv"

let s = try String(contentsOf: url)

I get the following error:

Error Domain=NSCocoaErrorDomain Code=260 "The file “x.csv” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/Users/userName/Documents⁩/MyAppName⁩/x.csv, NSUnderlyingError=0x7febf3c991b0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}

I know this is probably something ridiculously simple I am forgetting but I looked a lot for a solution and I couldn't find one. Any suggestions?

2
  • Barring that, does the playground allow you to read arbitrary files from the file system? I know that whenever I write an app to read a file, it always fails and then I have to add an entitlement for reading files. Jan 13, 2019 at 23:01
  • @user1118321: I tried running it in a terminal app but it gives me the same error.
    – aaHaa
    Jan 13, 2019 at 23:05

1 Answer 1

1

The issue is with this line:

let file = "Documents⁩/MyAppName⁩/x.csv"

You can't see it but between the s and / there is a hidden character - U+2069 - "POP DIRECTIONAL ISOLATE". Retype that line again without any special characters and your code will work.

That aside, here is how you should create the URL to the Documents folder.

let home = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

let file = "MyAppName⁩/x.csv"
13
  • Can you explain why this works despite the output of path appearing the same? This is fairly surprising to me. Jan 13, 2019 at 23:16
  • @user1118321 I'm trying to figure that out myself. But in the meantime, the code in my answer is the proper way to get access to a user's Documents folder.
    – rmaddy
    Jan 13, 2019 at 23:18
  • Thanks a lot for your reply! Unfortunately, I tried this but I am still getting the same error.
    – aaHaa
    Jan 13, 2019 at 23:28
  • I just updated my answer for the real problem. Please see the update.
    – rmaddy
    Jan 13, 2019 at 23:29
  • 1
    Oh wow! That's crazy! Good catch! I once had a problem where Xcode (or really any app, but it was only a problem in Xcode) would insert an invisible Cntl-P into my document whenever I hit the Windows key of the Windows keyboard I had connected to my Mac. Furthermore, Xcode didn't render anything for that character. I don't recall how I eventually figured out what was going on, but it was a hair-pulling experience! Jan 13, 2019 at 23:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.