#Palau: NSUserDefaults with Wings!
- Easily store your Custom Types in NSUSerDefaults
- Most Standard Types Out-of-the-box
- Per-property based Chainable Rules
- Supports NSCoding and RawRepresentable
- 300% Type Safe :P
- 100% Unit Test Coverage
- Swift 3 features coming!
- Bool
- Int
- UInt
- Float
- Double
- String
- Array
- Dictionary
- NSNumber
- NSString
- NSArray
- NSDictionary
- NSDate
- NSData
- NSURL
- NSIndexPath
- UIColor
- Swift 2.2
- iOS 8.0+ / tvOS 9.0+ / watchOS 2.0+
- Xcode 7.3+
To integrate Palau into your project using Carthage, add to your Cartfile
:
github "symentis/Palau" ~> 1.0
Run carthage update
to build the framework and drag the built Palau.framework
into your Xcode project.
See more instructions on the Carthage page.
Import Palau
import Palau
Once you import the framework you can setup PalauDefaults like:
extension PalauDefaults {
public static var name: PalauDefaultsEntry<String> {
/// backingName is the key used in NSUserDefaults
get { return value("backingName") }
set { }
}
}
By Default a Value will always be optional.
If you want to set a value you call:
PalauDefaults.name.value = "Iam a great String value!"
Getting your value back is as easy as:
/// name is an Optional<String>?
let name = PalauDefaults.name.value
You can delete a property by setting it to: Nil
PalauDefaults.name.value = nil
Or
/// skip custom rules and delete
PalauDefaults.name.reset()
If you want to provide a default, when there is no value set, you can write a custom rule.
We include two rule types by default: whenNil and ensure
import Palau
extension PalauDefaults {
public static var color: PalauDefaultsEntry<UIColor> {
/// Add as many chainable rules as you like
get { return value("color").whenNil(use: UIColor.redColor()) }
set { }
}
}
/// is UIColor.redColor()
let color: UIColor? = PalauDefaults.color.value
You can also build up arbitrary rules for your value like:
/// Custom Validator Closure
let lessThan10: Int? -> Bool = {
return $0.map { $0 < 10 } ?? false
}
public static var ensuredIntValue: PalauDefaultsEntry<Int> {
get { return value("ensuredIntValue")
.whenNil(use: 10)
/// when: takes a closure in this case our Custom Validator
.ensure(when: lessThan10, use: 10) }
set { }
}
/// try setting the property to 8
PalauDefaults.ensuredIntValue.value = 8
/// property ensured to be >= 10
assert(PalauDefaults.ensuredIntValue.value == 10)
In Swift 2.2 Classes and Protocols can be used to constrain the ValueType. For example this is how Palau adds support for RawRepresentable via an Extension:
/// Extension for RawRepresentable types aka enums
extension PalauDefaultable where ValueType: RawRepresentable {
public static func get(key: String, from defaults: NSUD) -> ValueType? {
guard let val = defaults.objectForKey(key) as? ValueType.RawValue else { return nil }
return ValueType(rawValue: val)
}
public static func set(value: ValueType?, forKey key: String, in defaults: NSUD) -> Void {
guard let value = value?.rawValue as? AnyObject else { return defaults.removeObjectForKey(key) }
defaults.setObject(value, forKey: key)
}
}
Generally for Types which conform to NSCoding you can usually just provide an extension like so:
/// Make UIColor PalauDefaultable
extension UIColor: PalauDefaultable {
public typealias ValueType = UIColor
}
We are waiting for extensions on Generic types.... yay!
Palau is named after the Palau swiftlet, a species of swift, endemic to the island of Palau.
Palau is owned and maintained by Symentis GmbH.
Developed by: Elmar Kretzer & Madhava Jay
Follow for more Swift Goodness:
##Logo
Awesome Logo by: 4th motion
Palau is released under the Apache 2.0 license. See LICENSE for details.