Developer Forums relocated!

Need help with Apple Developer tools and technologies? Want to share information with other developers and Apple engineers? Visit Developer Forums at Apple.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Xcode + Swift + OS X : How do I read a file?

Hello,


I am attempting to teach myself Swift using the Xcode environment.

I have been able to create an action in the code that connects to a button push. When I attempt to read a CSV file with String(contentsOfFile: path&filename) it fails. It appears that it is a permissions issue since the file resides on my desktop. In attempt to circumvent the issue I added an empty file to the project and copied my data into it. Then attempted to use String(contentsOfFile: "Data.csv") and same with "./Data.csv" to no avail. The error is no such file or directory.


Any input on how to fix this issue would be appreciated.


Below is my action.


Regards,


Chris


    @IBAction func sayButtonClicked(_ sender: Any) {


        


        do{


            let content = try String(contentsOfFile: "Data.csv")

            let parsedCSV: [[CustomStringConvertible]] = content


              .components(separatedBy: "\n")


              .map({$0.components(separatedBy: ",")


                  .map({


                    if let floatValue = Float($0) {


                      return floatValue


                    } else {


                      return $0


                    }


                  })


              })


            print(parsedCSV)


            }


        catch{


            print (error)


        }


    }  // end of select button action

iMac 27″, macOS 11.4

Posted on Aug 25, 2021 4:59 PM

Reply

Similar questions

8 replies

Aug 26, 2021 11:41 AM in response to XChrisAH

XChrisAH wrote:

Etresoft,

NSOpenPanel worked. It appears to pull the document over into the sandbox and allow me to read it.
Implementation was relatively easy.

That's good to hear.


Technically speaking, it doesn't put the document into the sandbox, it pokes a hole into the sandbox, respecting your user's intent, so that your sandboxed app can read the file.


However, I strongly suggest you read my rant about CSV that I just posted in this other thread. It's OK if you are just learning, but you never want to attempt to read CSV on your own. It seems simple, but it is practically impossible in the real world. If you wanted to use something that had real-world utility, I would suggest you save your input file in JSON format instead. JSON is a similar garbage file format, but it is loved by all the hip kids these days. Reading JSON using a strongly typed language like Swift poses some interesting problems.

Aug 26, 2021 4:25 AM in response to Keith Barkley

I don’t think your suggestion, or MrHoffman’s above, will work by default. They both use a full file path. That is the correct solution due to the ambiguous nature of the “current directory” in a GUI app. But the default settings for new projects in Xcode have the Sandbox enabled, which will not allow access to most user paths. Xcode is just not beginner-friendly.

Aug 26, 2021 11:49 AM in response to Keith Barkley

Keith Barkley wrote:

I have found when I save a file that it usually goes into the binary folder for the new application. You can then store files there and they should be found. Note that it has been a while (pre-sandbox) since I tried this.

To tell you the truth, I'm not even sure about the behaviour of NSOpenPanel with respect to the file-read entitlement. I have an app in the store right now that doesn't have any file-read entitlements, yet it still reads system files. I don't need NSOpenPanel or an entitlement to read them. It's tricky. My primary app right now is on iOS. There too, I can read files (and write) files with no entitlement. It's just the Mac with all these legacy APIs that makes things so tricky. I would not recommend anyone start developing on macOS these days.

Xcode + Swift + OS X : How do I read a file?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.