2 // KCManagedObject.swift
5 // Created by Hori,Masaki on 2017/02/02.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
12 enum KCManagedObjectError: Error {
17 class KCManagedObject: NSManagedObject {
19 private static let intValueKyes =
21 "api_enqflg", "api_aftershipid", "api_progress", "api_usebull",
22 "api_flagship", "api_name_id",
23 "api_comment_id", "api_nickname_id", "api_member_id",
24 "api_flag_0", "api_flag_1", "api_flag_2", "api_flag_3", "api_flag_4",
25 "api_flag_5", "api_flag_6", "api_flag_7",
29 override func validateValue(_ value: AutoreleasingUnsafeMutablePointer<AnyObject?>, forKey key: String) throws {
31 if value.pointee is NSNull {
38 if KCManagedObject.intValueKyes.contains(key) {
40 if let _ = value.pointee as? Int { return }
41 if let s = value.pointee as? String {
43 value.pointee = Int(s) as AnyObject?
48 print("KCManagedObject type \(type(of: value.pointee))")
50 throw KCManagedObjectError.invalid
54 override func value(forUndefinedKey key: String) -> Any? {
56 if key == "description" { return value(forKey: "description_") }
58 if key.hasPrefix("api_") {
60 let k = String(key[key.index(key.startIndex, offsetBy: 4)...])
62 return value(forKey: k)
65 print("Entity \(type(of: self).entityName) dose not have key \(key)")
70 override func setValue(_ value: Any?, forUndefinedKey key: String) {
72 if key == "description" {
74 setValue(value, forKey: "description_")
79 if key.hasPrefix("api_") {
81 let k = String(key[key.index(key.startIndex, offsetBy: 4)...])
82 setValue(value, forKey: k)
87 print("Entity \(type(of: self).entityName) dose not have key \(key)")