OSDN Git Service

不要となっていたプロパティを削除
[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 import Doutaku
12
13 enum KCManagedObjectError: Error {
14     
15     case invalid
16 }
17
18 class KCManagedObject: NSManagedObject {
19     
20     private static let intValueKyes: Set<String> = ["api_aftershipid", "api_level"]
21     
22     override func validateValue(_ value: AutoreleasingUnsafeMutablePointer<AnyObject?>, forKey key: String) throws {
23         
24         if value.pointee is NSNull {
25             
26             value.pointee = nil
27             
28             return
29         }
30         
31         if KCManagedObject.intValueKyes.contains(key) {
32             
33             if let _ = value.pointee as? Int {
34                 
35                 return
36             }
37             if let s = value.pointee as? String {
38                 
39                 value.pointee = Int(s) as AnyObject?
40                 
41                 return
42             }
43             
44             print("KCManagedObject type \(type(of: value.pointee))")
45             
46             throw KCManagedObjectError.invalid
47         }
48     }
49     
50     override func value(forUndefinedKey key: String) -> Any? {
51         
52         if key == "description" {
53             
54             return value(forKey: "description_")
55         }
56         
57         if key.hasPrefix("api_") {
58             
59             let k = String(key[key.index(key.startIndex, offsetBy: 4)...])
60             
61             return value(forKey: k)
62         }
63         
64         print("Entity \(String(describing: self)) dose not have key \(key)")
65         
66         return nil
67     }
68     
69     override func setValue(_ value: Any?, forUndefinedKey key: String) {
70         
71         if key == "description" {
72             
73             setValue(value, forKey: "description_")
74             
75             return
76         }
77         
78         if key.hasPrefix("api_") {
79             
80             let k = String(key[key.index(key.startIndex, offsetBy: 4)...])
81             setValue(value, forKey: k)
82             
83             return
84         }
85         
86         print("Entity \(String(describing: self)) dose not have key \(key)")
87     }
88 }