8000 GitHub - fuxx/Palau: Palau
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fuxx/Palau

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Palau: NSUserDefaults with Wings!

Build Status Carthage Compatible Platform

#Palau: NSUserDefaults with Wings!

Features

  • 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!

Already Included Types

Swift

  • Bool
  • Int
  • UInt
  • Float
  • Double
  • String
  • Array
  • Dictionary

Foundation

  • NSNumber
  • NSString
  • NSArray
  • NSDictionary
  • NSDate
  • NSData
  • NSURL
  • NSIndexPath
  • UIColor

Requirements

  • Swift 2.2
  • iOS 8.0+ / tvOS 9.0+ / watchOS 2.0+
  • Xcode 7.3+

Installation

Carthage

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.

Usage

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 { }
  }
}

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!"

Get

Getting your value back is as easy as:

/// name is an Optional<String>?
let name = PalauDefaults.name.value

Delete

You can delete a property by setting it to: Nil

PalauDefaults.name.value = nil

Or

/// skip custom rules and delete
PalauDefaults.name.reset()

Custom Rules

Providing a Default Value

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

Providing a Validator

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)

Custom Types

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
}

Limitations for Swift 2.2

We are waiting for extensions on Generic types.... yay!

FAQ

What's the origin of the name Palau?

Palau is named after the Palau swiftlet, a species of swift, endemic to the island of Palau.


Credits

Palau is owned and maintained by Symentis GmbH.

Developed by: Elmar Kretzer & Madhava Jay

Follow for more Swift Goodness: Twitter Twitter

##Logo

Awesome Logo by: 4th motion

String Test Fixtures

Markus Kuhn

License

Palau is released under the Apache 2.0 license. See LICENSE for details.

About

Palau

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 93.5%
  • HTML 4.8%
  • Objective-C 1.7%
0