4 Copyright (c) 2017, Hori, Masaki.
7 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
9 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
13 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 Created by 1024jp on 2016-01-03.
26 ------------------------------------------------------------------------------
30 Licensed under the Apache License, Version 2.0 (the "License");
31 you may not use this file except in compliance with the License.
32 You may obtain a copy of the License at
34 https://www.apache.org/licenses/LICENSE-2.0
36 Unless required by applicable law or agreed to in writing, software
37 distributed under the License is distributed on an "AS IS" BASIS,
38 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39 See the License for the specific language governing permissions and
40 limitations under the License.
46 class DefaultKeys: RawRepresentable, Hashable, CustomStringConvertible {
51 required init(rawValue: String) {
53 self.rawValue = rawValue
59 return self.rawValue.hashValue
63 var description: String {
70 final class DefaultKey<T>: DefaultKeys {
72 private let rawAlternativeValue: T?
74 let regulator: (T) -> T
76 init(_ rawValue: String, alternative value: T? = nil, regulator: @escaping (T) -> T = { $0 }) {
78 self.rawAlternativeValue = value
79 self.regulator = regulator
81 super.init(rawValue: rawValue)
85 required convenience init(rawValue: String) {
93 switch rawAlternativeValue {
95 case nil: fatalError("DefaultKey (\(self)) has no alternative value.")
97 case let .some(value): return value
105 extension UserDefaults {
107 subscript(key: DefaultKey<Bool>) -> Bool {
109 get { return self.bool(forKey: key.rawValue) }
110 set { self.set(newValue, forKey: key.rawValue) }
114 subscript(key: DefaultKey<Int>) -> Int {
116 get { return key.regulator(self.integer(forKey: key.rawValue)) }
117 set { self.set(key.regulator(newValue), forKey: key.rawValue) }
121 subscript(key: DefaultKey<Double>) -> Double {
123 get { return key.regulator(self.double(forKey: key.rawValue)) }
124 set { self.set(key.regulator(newValue), forKey: key.rawValue) }
128 subscript(key: DefaultKey<CGFloat>) -> CGFloat {
130 get { return key.regulator(CGFloat(self.double(forKey: key.rawValue))) }
131 set { self.set(key.regulator(newValue), forKey: key.rawValue) }
135 subscript(key: DefaultKey<String?>) -> String? {
137 get { return self.string(forKey: key.rawValue) }
138 set { self.set(newValue, forKey: key.rawValue) }