Last active
August 22, 2018 03:44
-
-
Save sakurabird/df1eeb5f738e149c6beff8634d6bc6e2 to your computer and use it in GitHub Desktop.
Swift4.0 Codable + Realm (CodableでJsonをパース→RealmでDBに投入)
This file contains hidden or 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
// | |
// Kind.swift | |
// ExamRealm1 | |
// | |
// Created by Sakura on 2018/01/22. | |
// Copyright © 2018年 Sakura. All rights reserved. | |
// | |
import Foundation | |
import RealmSwift | |
class Kind : RealmSwift.Object, Codable { | |
@objc dynamic var id: Int = 0 | |
@objc dynamic var name: String? | |
@objc dynamic var typeId: Int = 0 | |
enum CodingKeys: String, CodingKey { | |
case id = "id" | |
case name = "name" | |
case typeId = "type_id" | |
} | |
required convenience init(from decoder: Decoder) throws { | |
self.init() | |
let values = try decoder.container(keyedBy: CodingKeys.self) | |
id = (try values.decodeIfPresent(Int.self, forKey: .id))! | |
name = try values.decodeIfPresent(String.self, forKey: .name) | |
typeId = (try values.decodeIfPresent(Int.self, forKey: .typeId))! | |
} | |
override static func primaryKey() -> String? { | |
return "id" | |
} | |
} |
This file contains hidden or 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
{ | |
"kinds": [ | |
{ | |
"id": 1, | |
"name": "穀類", | |
"type_id": 1 | |
}, | |
{ | |
"id": 2, | |
"name": "豆・種実・藻類", | |
"type_id": 1 | |
}, | |
{ | |
"id": 3, | |
"name": "いも・野菜・きのこ類", | |
"type_id": 1 | |
}, | |
{ | |
"id": 4, | |
"name": "果実類", | |
"type_id": 1 | |
}, | |
{ | |
"id": 5, | |
"name": "魚介類", | |
"type_id": 1 | |
}, | |
{ | |
"id": 6, | |
"name": "肉類", | |
"type_id": 1 | |
}, | |
{ | |
"id": 7, | |
"name": "卵・乳類", | |
"type_id": 1 | |
}, | |
{ | |
"id": 8, | |
"name": "油脂類・調味料・香辛料", | |
"type_id": 1 | |
}, | |
{ | |
"id": 9, | |
"name": "砂糖・ジャムなど", | |
"type_id": 1 | |
}, | |
{ | |
"id": 10, | |
"name": "主食(ごはん・パンなど)", | |
"type_id": 2 | |
} | |
] | |
} |
This file contains hidden or 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
// | |
// Kinds.swift | |
// ExamRealm1 | |
// | |
// Created by Sakura on 2018/01/22. | |
// Copyright © 2018年 Sakura. All rights reserved. | |
// | |
import Foundation | |
class Kinds : Codable { | |
let kinds : [Kind]? | |
enum CodingKeys: String, CodingKey { | |
case kinds = "kinds" | |
} | |
required init(from decoder: Decoder) throws { | |
let values = try decoder.container(keyedBy: CodingKeys.self) | |
kinds = try values.decodeIfPresent([Kind].self, forKey: .kinds) | |
} | |
} |
This file contains hidden or 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
// | |
// ViewController.swift | |
// ExamRealm1 | |
// | |
// Created by Sakura on 2018/01/19. | |
// Copyright © 2018年 Sakura. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
import RealmSwift | |
class ViewController: UIViewController { | |
// var realm: Realm! | |
var kinds: [Kind]? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
loadJsonFile() | |
saveDB() | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
func loadJsonFile() { | |
guard let path = Bundle.main.path(forResource: "kinds", ofType: "json") else { return } | |
let url = URL(fileURLWithPath: path) | |
do { | |
let data = try Data(contentsOf: url) | |
let kinds = try JSONDecoder().decode(Kinds.self, from: data) | |
for kind in (kinds.kinds)!{ | |
print("(json)kind.id:\(kind.id) name:\(kind.name ?? "null") typeId:\(kind.typeId)") | |
} | |
self.kinds = kinds.kinds | |
} catch { | |
print(error) | |
} | |
} | |
func saveDB() { | |
let realm = try! Realm() | |
try! realm.write { | |
realm.deleteAll() | |
for kind in self.kinds! { | |
realm.add(kind) | |
} | |
} | |
print(realm.objects(Kind.self)) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment