[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