4

I'm just starting learning Swift and to teach myself I'm making a simple command line app. It will eventually connect to an online data source but initially I want to load data from a file. I've seen various guides on reading the contents of a file in Swift but none of them seem to work for me. Here is my app so far:

import Foundation // Set the file path let path = "/Users⁩/username/workspace⁩/⁨Swift⁩/sis⁩/sis/data.json⁩" do { // Get the contents let contents = try String(contentsOfFile: path, encoding: .utf8) print(contents) } catch let error as NSError { print("Ooops! Something went wrong: \(error)") } 

Running it outputs:

Ooops! Something went wrong: Error Domain=NSCocoaErrorDomain Code=260 "The file “data.json⁩” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/Users⁩/username/workspace⁩/⁨Swift⁩/sis⁩/sis/data.json⁩, NSUnderlyingError=0x100e19a50 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}} 

However on the terminal:

$ ls -l /Users/username/workspace/Swift/sis/sis/data.json -rwxrwxrwx@ 1 username staff 165563 16 Jan 17:14 /Users/username/workspace/Swift/sis/sis/data.json 

(yeah I relaxed the permissions somewhat just in case that was the problem)

The only slightly anomalous thing I noticed (aside from the inaccurate assertion that the file doesn't exist) was that when I copy and past the path from the XCode output into iTerm2 it puts spaces between each path component:

path with spaces

(pasted as an image as copying it and pasting it back into this form seems to hide the spaces - this is probably irrelevant anyway)

Any help figuring this out would be really appreciated!

2
  • 1
    Your code looks correct, but there are indeed some invisible characters in your let path = ... line. Deleting and retyping that line should fix the problem. Commented Jan 18, 2019 at 10:06
  • 1
    You can also print(Array(path.unicodeScalars)) to see the cause of the problem. Commented Jan 18, 2019 at 10:17

2 Answers 2

5

I copied your code, downloaded a sample json file to my desktop, and renamed it to example_ 1.json (I included a space inside the file name).

import Foundation // Set the file path let path = "/Users⁩/username/Desktop/example_ 1.json⁩" do { // Get the contents let contents = try String(contentsOfFile: path, encoding: .utf8) print(contents) } catch let error as NSError { print("Ooops! Something went wrong: \(error)") } 

It successfully printed the file. It also worked when I defined contents as a NSString.

let contents = try NSString(contentsOfFile: path, encoding: String.Encoding.ascii.rawValue) 

I am using Swift 4.2.1

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much for trying this. I tried creating a new file on the desktop and referencing that instead but got a similar error for that file. I assume that there is something wrong with my system / environment but I am not sure where to start...
I'm not sure if this helps but what version of Swift are you using? Run "swift -v" to find out.
2

you can not read if your command line app is sandboxed. what you can do is to add this file in your project and set path of file by looking the full path of file in identity inspector. enter image description here

let path = "/Users/snx/EmailReplacer/EmailReplacer/shared_domains_staging.json" do { let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe) let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves) if let jsonResult = jsonResult as? Dictionary<String, AnyObject> { print(jsonResult) } } catch { print(error) } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.