2015-06-29 26 views
1

這有點難以解釋,我希望能夠從iOS應用程序中的某些文件中讀取我在計算機上的文件。我將這些文件存儲在與我的'ViewController.swift'文件相同的目錄中。讀取存儲在Xcode項目中的文件

enter image description here

我的問題是,如果我想從文件「testData.csv」,我應該如何去了解我的應用程序引用它來閱讀?

回答

1

獲取文件的URL

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

然後你就可以使用這個快捷LIB解析文件 https://github.com/naoty/SwiftCSV

3

試試這個:

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

爲了使代碼通用的,可重複使用,你可以定義一個函數來接受你的文件名以及文件類型。

快樂編碼。