Skip to content

Instantly share code, notes, and snippets.

@ryokwkm ryokwkm/appDelegate.swift
Last active Dec 17, 2015

Embed
What would you like to do?
[swift2.0] CSVデータによるCoreDataの初期化[MagicalRecord]
extension String : CollectionType{}
class AppDelegate: UIResponder, UIApplicationDelegate {
...
//DB初期データ
func setDefaultData() {
var memos:[Memo]! = Memo.MR_findAll() as! [Memo]
if let first = memos?.first {
print("** Default data is already set up **")
} else {
print("** Default data is empty !! ** ")
let filePath = NSBundle.mainBundle().pathForResource("Memo", ofType: "csv")
do {
let data = try NSString(contentsOfFile: filePath!, encoding: NSUTF8StringEncoding) as String
//memo保存
data.enumerateLines{ (line, stop) -> () in
let item:[String] = line.split{
$0 == ","
}
let 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()
}
} catch _{
print( "err")
}
}
}
}
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.