OSDN Git Service

swiftlint 'line_length'の警告を修正
[kcd/KCD.git] / KCD / KCManagedObject.swift
1 //
2 //  KCManagedObject.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/02/02.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Foundation
10 import CoreData
11
12 enum KCManagedObjectError: Error {
13     case invalid
14 }
15
16 class KCManagedObject: NSManagedObject {
17     private static let intValueKyes =
18         [
19     "api_enqflg", "api_aftershipid", "api_progress", "api_usebull",
20     "api_flagship", "api_name_id",
21     "api_comment_id", "api_nickname_id", "api_member_id",
22     "api_flag_0", "api_flag_1", "api_flag_2", "api_flag_3", "api_flag_4",
23     "api_flag_5", "api_flag_6", "api_flag_7",
24     "api_level"
25     ]
26     override func validateValue(_ value: AutoreleasingUnsafeMutablePointer<AnyObject?>, forKey key: String) throws {
27         if value.pointee is NSNull {
28             value.pointee = nil
29             return
30         }
31         if KCManagedObject.intValueKyes.contains(key) {
32             if let _ = value.pointee as? Int { return }
33             if let s = value.pointee as? String {
34                 value.pointee = Int(s) as AnyObject?
35                 return
36             }
37             print("KCManagedObject type \(type(of: value.pointee))")
38             throw KCManagedObjectError.invalid
39         }
40     }
41     
42     override func value(forUndefinedKey key: String) -> Any? {
43         if key == "description" { return value(forKey: "description_") }
44         if key.hasPrefix("api_") {
45             let four = key.index(key.startIndex, offsetBy: 4)
46             let k = key[four..<key.endIndex]
47             return value(forKey: k)
48         }
49         print("Entity \(entity.name) dose not have key \(key)")
50         return nil
51     }
52     override func setValue(_ value: Any?, forUndefinedKey key: String) {
53         if key == "description" {
54             setValue(value, forKey: "description_")
55             return
56         }
57         if key.hasPrefix("api_") {
58             let four = key.index(key.startIndex, offsetBy: 4)
59             let k = key[four..<key.endIndex]
60             setValue(value, forKey: k)
61             return
62         }
63         print("Entity \(entity.name) dose not have key \(key)")
64     }
65 }