32
29

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SwiftでCSVファイルを読み込み→配列に格納する

Last updated at Posted at 2015-05-19
  1. CSVファイルをプロジェクト内にドラック&ドロップして取り込む
  2. 下記コードで取り出し、配列に変換する
var result: [[String]] = []
if let csvPath = NSBundle.mainBundle().pathForResource("resource", ofType: "csv") {
    let csvString = NSString(contentsOfFile: csvPath, encoding: NSUTF8StringEncoding, error: nil) as! String
    csvString.enumerateLines { (line, stop) -> () in
        result.append(line.componentsSeparatedByString(","))
    }
}
println(result)

version:swift1.2

※ 下記の@takabosoft さんのコメントにあるようにデータにカンマが含まれていたり、不明なcsvを扱う場合には不十分ですので、ご注意ください。

32
29
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
29

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?