Skip to content

Instantly share code, notes, and snippets.

@ryokwkm
Last active December 17, 2015 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryokwkm/8a7ec73f0f1d1626f910 to your computer and use it in GitHub Desktop.
Save ryokwkm/8a7ec73f0f1d1626f910 to your computer and use it in GitHub Desktop.
[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