Last active
December 17, 2015 12:03
-
-
Save ryokwkm/8a7ec73f0f1d1626f910 to your computer and use it in GitHub Desktop.
[swift2.0] CSVデータによるCoreDataの初期化[MagicalRecord]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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