2

this is a little hard to explain, I want to be able to read from some files which I have on my computer in an iOS app. I am storing the files in the same directory as my 'ViewController.swift' file.

enter image description here

My question is, if I want to read from the file "testData.csv", how should I go about referencing it in my app?

3 Answers 3

4

Try this:

let file = NSBundle.mainBundle().pathForResource("testData", ofType: "cvs") var contents = String(contentsOfFile: path!, encoding: NSUTF8StringEncoding, error: nil)! 
Sign up to request clarification or add additional context in comments.

Comments

2
let bundlePath = NSBundle.mainBundle().pathForResource("testdata", ofType: "csv") var textContent = String(contentsOfFile: path!, encoding: NSUTF8StringEncoding, error: nil)! println(textContent) 

To make the code generic and reusable, You can define a function which accepts your file name as well file type.

Happy coding.

Comments

1

Get file URL

let fileUrl = NSBundle.mainBundle().URLForResource("testData", withExtension: "csv") 

Then you can parse the file using this swift lib https://github.com/naoty/SwiftCSV

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.