Swift에서 CoreData에 필요한 초기 데이터(마스터) 준비
CoraData 사용
CoreData의 Enities
plist 파일 만들기
추가된 plist 디렉토리
plist에 데이터 등록
등록해 본 샘플
plist에서 데이터 등록 데이터 읽기
func initMasters() {
println("initMasters ------------")
// plist の読み込み
let path:NSString = NSBundle.mainBundle().pathForResource("Masterdata", ofType: "plist")!
var masterDataDictionary:NSDictionary = NSDictionary(contentsOfFile: path)
let app: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let categoryContext: NSManagedObjectContext = app.managedObjectContext!
for(var i = 1; i<=masterDataDictionary.count; i++) {
let index_name: String = "Category" + String(i)
var item: AnyObject = masterDataDictionary[index_name]!
println(item["name"])
let categoryEntity: NSEntityDescription! = NSEntityDescription.entityForName(
"Categories",
inManagedObjectContext: categoryContext
)
var new_data = NSManagedObject(entity: categoryEntity, insertIntoManagedObjectContext: categoryContext)
//new_data.name = item["name"] as String
new_data.setValue(item["name"] as String, forKey: "name")
var error: NSError?
categoryContext.save(&error)
}
println("------------")
}
데이터가 있으면 실행하지 않음
초기 데이터는 한 번만 로그인하면 됩니다. 데이터가 존재하지 않으면 투입됩니다. (저는 sqlite 파일도 있다고 생각합니다.) override func viewDidLoad() {
super.viewDidLoad()
var results:NSArray = readData()
if(results.count == 0) {
// 初期データーの投入
initMasters()
results = readData()
}
}
전체 // ViewController.swift
// TestAppCoreDataInitApp01
import UIKit
import CoreData
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var results:NSArray = readData()
if(results.count == 0) {
// 初期データーの投入
initMasters()
results = readData()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func readData() -> NSArray{
println("readData ------------")
let app: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let categoryContext: NSManagedObjectContext = app.managedObjectContext!
let categoryRequest: NSFetchRequest = NSFetchRequest(entityName: "Categories")
var results: NSArray! = categoryContext.executeFetchRequest(categoryRequest, error: nil)
for data in results {
println("------------")
println(data.name?)
println("------------")
}
return results
}
func initMasters() {
println("initMasters ------------")
// plist の読み込み
let path:NSString = NSBundle.mainBundle().pathForResource("Masterdata", ofType: "plist")!
var masterDataDictionary:NSDictionary = NSDictionary(contentsOfFile: path)
let app: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let categoryContext: NSManagedObjectContext = app.managedObjectContext!
for(var i = 1; i<=masterDataDictionary.count; i++) {
let index_name: String = "Category" + String(i)
var item: AnyObject = masterDataDictionary[index_name]!
println(item["name"])
let categoryEntity: NSEntityDescription! = NSEntityDescription.entityForName(
"Categories",
inManagedObjectContext: categoryContext
)
var new_data = NSManagedObject(entity: categoryEntity, insertIntoManagedObjectContext: categoryContext)
//new_data.name = item["name"] as String
new_data.setValue(item["name"] as String, forKey: "name")
var error: NSError?
categoryContext.save(&error)
}
println("------------")
}
}
운영 환경
override func viewDidLoad() {
super.viewDidLoad()
var results:NSArray = readData()
if(results.count == 0) {
// 初期データーの投入
initMasters()
results = readData()
}
}
// ViewController.swift
// TestAppCoreDataInitApp01
import UIKit
import CoreData
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var results:NSArray = readData()
if(results.count == 0) {
// 初期データーの投入
initMasters()
results = readData()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func readData() -> NSArray{
println("readData ------------")
let app: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let categoryContext: NSManagedObjectContext = app.managedObjectContext!
let categoryRequest: NSFetchRequest = NSFetchRequest(entityName: "Categories")
var results: NSArray! = categoryContext.executeFetchRequest(categoryRequest, error: nil)
for data in results {
println("------------")
println(data.name?)
println("------------")
}
return results
}
func initMasters() {
println("initMasters ------------")
// plist の読み込み
let path:NSString = NSBundle.mainBundle().pathForResource("Masterdata", ofType: "plist")!
var masterDataDictionary:NSDictionary = NSDictionary(contentsOfFile: path)
let app: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let categoryContext: NSManagedObjectContext = app.managedObjectContext!
for(var i = 1; i<=masterDataDictionary.count; i++) {
let index_name: String = "Category" + String(i)
var item: AnyObject = masterDataDictionary[index_name]!
println(item["name"])
let categoryEntity: NSEntityDescription! = NSEntityDescription.entityForName(
"Categories",
inManagedObjectContext: categoryContext
)
var new_data = NSManagedObject(entity: categoryEntity, insertIntoManagedObjectContext: categoryContext)
//new_data.name = item["name"] as String
new_data.setValue(item["name"] as String, forKey: "name")
var error: NSError?
categoryContext.save(&error)
}
println("------------")
}
}
운영 환경
참고 자료
Reference
이 문제에 관하여(Swift에서 CoreData에 필요한 초기 데이터(마스터) 준비), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/matsuhisa_h/items/741d7b1c2541ea6fc68d텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)