diff --git a/Defaults+RegisterDefaults.swift b/Defaults+RegisterDefaults.swift new file mode 100644 index 00000000..c445a6e2 --- /dev/null +++ b/Defaults+RegisterDefaults.swift @@ -0,0 +1,63 @@ +// +// SwiftyUserDefaults +// +// Copyright (c) 2015-present Radosław Pietruszewski, Łukasz Mróz +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +import Foundation + +public protocol DefaultsKeyable { + var _key: String { get } + var _defaultValue: Any? { get } +} + +extension DefaultsKey: DefaultsKeyable { + public var _defaultValue: Any? { + return self.defaultValue as Any? + } +} + +public extension UserDefaults { + func register(defaultsKeys: [DefaultsKeyable]) { + self.register(defaults: defaultsKeys.dictionaryWithDefaultValues()) + } +} + +public extension Array where Element == DefaultsKeyable { + /// - returns: Dictionary with pairs of `DefaultsKey` keys and default values. Does not contain keys + /// where the default value is `nil`. + func dictionaryWithDefaultValues() -> [String : Any] { + return self + .filter { $0._defaultValue != nil } + .mapDictionary { ($0._key, $0._defaultValue!) } + } +} + +internal extension Array { + func mapDictionary(_ transform: (Element) -> ((K, V))) -> [K : V] { + var result: [K : V] = [:] + for element in self { + let (key, value) = transform(element) + result[key] = value + } + return result + } +} diff --git a/SwiftyUserDefaults.xcodeproj/project.pbxproj b/SwiftyUserDefaults.xcodeproj/project.pbxproj index 1cb719c4..7b55a95d 100644 --- a/SwiftyUserDefaults.xcodeproj/project.pbxproj +++ b/SwiftyUserDefaults.xcodeproj/project.pbxproj @@ -17,6 +17,7 @@ 311F24C3509BE924078AD67E /* DefaultsObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9701991125BC1345631E831E /* DefaultsObserver.swift */; }; 499B0191487C22E03028ED3C /* Defaults+Int.swift in Sources */ = {isa = PBXBuildFile; fileRef = 041FE3F6B58F219CAAE0AD64 /* Defaults+Int.swift */; }; 4B604921053C4F31217DCE9E /* Defaults+Observing.swift in Sources */ = {isa = PBXBuildFile; fileRef = F910BE3FABE99289E0545D95 /* Defaults+Observing.swift */; }; + 50B63B342265BBE300CCB151 /* Defaults+RegisterDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50B63B332265BBE300CCB151 /* Defaults+RegisterDefaults.swift */; }; 7EE5BB8911DFD47B6D6ED8E5 /* Defaults+Date.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F97C9DDE34857798EA6519B /* Defaults+Date.swift */; }; 80C1C495919531D69B34BA6F /* Defaults+Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78703A0F180A4B7C450CD191 /* Defaults+Data.swift */; }; 88573499C0B9C4F466D7EC2D /* DefaultsSerializable.swift in Sources */ = {isa = PBXBuildFile; fileRef = F98C7EF97445788836162CD0 /* DefaultsSerializable.swift */; }; @@ -55,6 +56,7 @@ 2EFFE22277FA6C163E34F4D2 /* DefaultsSerializableSpec.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = DefaultsSerializableSpec.swift; path = Tests/SwiftyUserDefaultsTests/TestHelpers/DefaultsSerializableSpec.swift; sourceTree = ""; }; 2F97C9DDE34857798EA6519B /* Defaults+Date.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = "Defaults+Date.swift"; path = "Tests/SwiftyUserDefaultsTests/Built-ins/Defaults+Date.swift"; sourceTree = ""; }; 3D8980553F1615D4ACB4AA43 /* Defaults+Dictionary.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = "Defaults+Dictionary.swift"; path = "Tests/SwiftyUserDefaultsTests/Built-ins/Defaults+Dictionary.swift"; sourceTree = ""; }; + 50B63B332265BBE300CCB151 /* Defaults+RegisterDefaults.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Defaults+RegisterDefaults.swift"; sourceTree = ""; }; 63C197A55B797D98D9603C98 /* UserDefaults+PropertyList.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = "UserDefaults+PropertyList.swift"; path = "Tests/SwiftyUserDefaultsTests/TestHelpers/UserDefaults+PropertyList.swift"; sourceTree = ""; }; 6494A812C2386E7351405E72 /* Defaults+URL.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = "Defaults+URL.swift"; path = "Tests/SwiftyUserDefaultsTests/Built-ins/Defaults+URL.swift"; sourceTree = ""; }; 6C48B95D407E4F720507F3D8 /* Info.plist */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.plist.xml; name = Info.plist; path = Sources/Info.plist; sourceTree = ""; }; @@ -122,6 +124,7 @@ 85096B65E07A3BC7F856F0D1 /* OptionalType.swift */, F98C7EF97445788836162CD0 /* DefaultsSerializable.swift */, EE155E06F856DFEF78FD63F7 /* Defaults.swift */, + 50B63B332265BBE300CCB151 /* Defaults+RegisterDefaults.swift */, ); name = Sources; sourceTree = ""; @@ -264,6 +267,7 @@ developmentRegion = English; hasScannedForEncodings = 0; knownRegions = ( + English, en, ); mainGroup = 360E42BFF4D0B60A2966BBFB; @@ -345,6 +349,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 50B63B342265BBE300CCB151 /* Defaults+RegisterDefaults.swift in Sources */, D102C1011A4314AFF773DCD5 /* Defaults+StringToBool.swift in Sources */, 037280C1C6C68BDB8E3759E6 /* DefaultsKeys.swift in Sources */, CC4CF6A63F6AA1B932D6757E /* DefaultsBridges.swift in Sources */,