Releases: phimage/Prephirences
Releases · phimage/Prephirences
3.0.0
2.2.1
- some utility functions to specify a default value when the stored value match a condition
public var intValueMin10: MutablePreference<Int> {
get {
return userDefaults.preferenceForKey("intKey")
.whenNil(use: 100)
.ensure(when: lessThan100, use: 100)
}
set {..}
}
- and... new protocol
PreferenceTransformation
: Before storing or accessing the value, transformation could be applied. This allow to archive, to change type, return default value if nil and many more.
userDefaults["aKey", myTransformation] = myObject
TransformationKey
implement this protocol. transfomation
attribute of MutablePreference
has type changed.
If you use before aPref.transformation = .Archive
. Now you must use aPref.transformation = TransformationKey.Archive
or aPref.transformationKey = Transformation.Archive
-
Utility functions for RawRepresentable objects
- get an
RawRepresentable
object withrawRepresentableForKey
and set withsetRawValue
- For
RawRepresentable
objects likeenum
you can use the computed attributepreferenceTransformation
astransformation
enum PrefEnum: String { case One, Two, Three} var pref: MutablePreference<PrefEnum> = preferences <| "enumKey" pref.transformation = PrefEnum.preferenceTransformation pref.value = PrefEnum.Two
- get an
2.2.0
2.0.8
NSCoder
implement MutablePreferencesType
init?(coder decoder: NSCoder) {
self.init()
self.intVar = decoder["intVarKey"] as? Int ?? 0
// or self.intVar = decoder.integerForKey("intVar")
self.stringVar = decoder["stringVarKey"] as? String ?? ""
}
func encodeWithCoder(coder: NSCoder) {
coder["intVarKey"] = self.intVar
coder["stringVarKey"] = self.stringVar
}
2.0.5
Allow to adapt a collection to PreferencesType
- using two closures, one to get key, the other to get the value from the collection element
struct Couple {
var name: String
var value: AnyObject
}
let collection: [Couple] = ...
let preferences = CollectionPreferencesAdapter(
collection: collection,
mapKey: {$0.name}
mapValue: {$0.value}
)
For instance NSHTTPCookieStorage
which contains a collection of NSHTTPCookie
conform now to protocol PreferencesType
2.0.4
2.0.3
- Operator or subscript to archive/convert object
userDefaults["mycolorkey", .Archive] = UIColor.blueColor()
- any object could conform to
PreferencesType
by extendingReflectingPreferences
struct PreferenceStruct {
var color: String = "red"
var age: Int
let enabled: Bool = true
}
extension PreferenceStruct: ReflectingPreferences {}
if pref["color"] as? String { .. }
PreferencesController
to allow binding on allPreferencesType
(inspired fromNSUserDefaultsController
) for OSX