5 // Created by Hori,Masaki on 2017/02/25.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
11 class AirBaseMapper: JSONMapper {
12 let apiResponse: APIResponse
13 let configuration = MappingConfiguration(entityType: AirBase.self,
14 dataKey: "api_data.api_air_base",
15 compositPrimaryKeys: ["area_id", "rid"],
16 editorStore: ServerDataStore.oneTimeEditor())
18 required init(_ apiResponse: APIResponse) {
19 self.apiResponse = apiResponse
22 func handleExtraValue(_ value: Any, forKey key: String, to object: NSManagedObject) -> Bool {
23 if key != "api_plane_info" { return false }
25 guard let airbase = object as? AirBase else {
26 print("object is not AirBase")
29 if airbase.planeInfo.count == 0 {
30 if let store = configuration.editorStore as? ServerDataStore {
31 let new: [AirBasePlaneInfo] = (0..<4).flatMap {_ in
32 store.createAirBasePlaneInfo()
34 airbase.planeInfo = NSOrderedSet(array: new)
38 guard let planeInfos = value as? [[String: Any]]
40 print("value is wrong")
43 guard let infos = airbase.planeInfo.array as? [AirBasePlaneInfo]
44 else { print("airbase is wrong")
47 zip(infos, planeInfos).forEach { (info, dict) in
48 guard let slotid = dict["api_slotid"] as? Int,
51 guard let cond = dict["api_cond"] as? Int,
52 let count = dict["api_count"] as? Int,
53 let maxCount = dict["api_max_count"] as? Int,
54 let squadronid = dict["api_squadron_id"] as? Int,
55 let state = dict["api_state"] as? Int
56 else { return print("planeInfos is wrong") }
59 info.max_count = maxCount
61 info.squadron_id = squadronid
63 info.airBase = airbase