From f0536bccea8e8e12adabb4f22b2a0d4e1b8fef39 Mon Sep 17 00:00:00 2001 From: "Takuto NAKAMURA (Kyome)" Date: Fri, 4 Nov 2022 17:15:46 +0900 Subject: [PATCH] SpiceKeyData supports Codable --- .../xcschemes/xcschememanagement.plist | 4 +- README.md | 2 + Sources/SpiceKey/SpiceKeyData.swift | 39 +++++++++++++++++-- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/Demo/SpiceKeyDemo.xcodeproj/xcuserdata/takuto.xcuserdatad/xcschemes/xcschememanagement.plist b/Demo/SpiceKeyDemo.xcodeproj/xcuserdata/takuto.xcuserdatad/xcschemes/xcschememanagement.plist index 3e8805a..8380a6b 100644 --- a/Demo/SpiceKeyDemo.xcodeproj/xcuserdata/takuto.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/Demo/SpiceKeyDemo.xcodeproj/xcuserdata/takuto.xcuserdatad/xcschemes/xcschememanagement.plist @@ -12,12 +12,12 @@ SpiceKeyDemo_AppKit.xcscheme_^#shared#^_ orderHint - 0 + 1 SpiceKeyDemo_SwiftUI.xcscheme_^#shared#^_ orderHint - 1 + 0 diff --git a/README.md b/README.md index 5b5234e..d7c08c5 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,8 @@ spiceKey.unregister() - Save shortcut +SpiceKeyData supports NSCoding & Codable. + ```swift let key = Key.a let flags = ModifierFlags.optCmd diff --git a/Sources/SpiceKey/SpiceKeyData.swift b/Sources/SpiceKey/SpiceKeyData.swift index dbda969..50dcb25 100644 --- a/Sources/SpiceKey/SpiceKeyData.swift +++ b/Sources/SpiceKey/SpiceKeyData.swift @@ -6,9 +6,18 @@ // Copyright © 2019 Takuto Nakamura. All rights reserved. // -import Foundation.NSObject +import Foundation + +open class SpiceKeyData: NSObject, NSCoding, Codable { + public enum CodingKeys: String, CodingKey { + case primaryKey + case keyCode + case control + case option + case shift + case command + } -open class SpiceKeyData: NSObject, NSCoding { public var primaryKey: String public var keyCode: CGKeyCode public var control: Bool @@ -56,7 +65,8 @@ open class SpiceKeyData: NSObject, NSCoding { self.command = modifierFlags.containsCommand self.spiceKey = spiceKey } - + + // NSCoding required public init?(coder: NSCoder) { primaryKey = (coder.decodeObject(forKey: "primaryKey") as? String) ?? "" keyCode = coder.decodeObject(forKey: "keyCode") as! CGKeyCode @@ -65,7 +75,7 @@ open class SpiceKeyData: NSObject, NSCoding { shift = coder.decodeBool(forKey: "shift") command = coder.decodeBool(forKey: "command") } - + public func encode(with coder: NSCoder) { coder.encode(primaryKey, forKey: "primaryKey") coder.encode(keyCode, forKey: "keyCode") @@ -74,4 +84,25 @@ open class SpiceKeyData: NSObject, NSCoding { coder.encode(shift, forKey: "shift") coder.encode(command, forKey: "command") } + + // Codable + public required init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + primaryKey = try container.decode(String.self, forKey: .primaryKey) + keyCode = try container.decode(CGKeyCode.self, forKey: .keyCode) + control = try container.decode(Bool.self, forKey: .control) + option = try container.decode(Bool.self, forKey: .option) + shift = try container.decode(Bool.self, forKey: .shift) + command = try container.decode(Bool.self, forKey: .command) + } + + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(primaryKey, forKey: .primaryKey) + try container.encode(keyCode, forKey: .keyCode) + try container.encode(control, forKey: .control) + try container.encode(option, forKey: .option) + try container.encode(shift, forKey: .shift) + try container.encode(command, forKey: .command) + } }