Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ファイルを読み込む
//Memoテーブルに保存。
//構造は title:String, value:String
var memos:[Memo]! = Memo.MR_findAll() as! [Memo]
if let first = memos?.first {
//データが存在した場合は初期化しない
} else {
//データ取得できなかった場合、初期データ挿入
let filePath = NSBundle.mainBundle().pathForResource("Memo", ofType: "csv")
let data = NSString(contentsOfFile: filePath!, encoding: NSUTF8StringEncoding, error: nil) as! String
data.enumerateLines{(line, stop) -> () in
let item:[String] = split(line) {
$0 == ","
}
var newRecord:Memo = Memo.MR_createEntity() as! Memo
newRecord.title = item[0]
if item.count > 1 {
newRecord.value = item[1]
} else {
newRecord.value = ""
}
newRecord.managedObjectContext!.MR_saveToPersistentStoreAndWait()
}
}
let filePath = NSBundle.mainBundle().pathForResource("Memo", ofType: "csv")
let data = NSString(contentsOfFile: filePath!, encoding: NSUTF8StringEncoding, error: nil) as! String
data.enumerateLines{(line, stop) -> () in
//カンマ区切りで出力(配列に格納まで)
let item:[String] = split(line) {
$0 == ","
}
println( item )
}
let filePath = NSBundle.mainBundle().pathForResource("Memo", ofType: "csv")
let data = NSString(contentsOfFile: filePath!, encoding: NSUTF8StringEncoding, error: nil) as! String
data.enumerateLines{(line, stop) -> () in
println(line)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.