Latest 0.1.2
Homepage https://github.com/claudetech/swift-box
License MIT
Platforms ios 8.0, osx 10.9, requires ARC
Frameworks Foundation
Authors

Swift box implementation

A box implementation for Swift.

This is useful when some values can contain errors, for example when
fetching some model from an API.

Installation

Add

pod 'SwiftBox'

to your PodFile.

Usage

You can create a Box by using

let fullBox = Box<String>("foo")
let failureBox = Box<String>(NSError(domain: "foo", code: 2, userInfo: nil)) // will be failure
let emptyBox = Box<String>.Empty

You can then use your box:

if box.isFull() {
    let myString = box>! // == box.value!
}
if box.isFailure() {
    let myError = box.failure!
}

if let str = box.value {
    useMyString(str)
}

switch box {
case Box.Full(let wrappedStr):
    useMyString(wrappedStr.value) // == wrappedStr.v
case Box.Failure(let err):
    handleError(err)
case Box.Empty:
    break
}

Example

class User {
    var name: String

    init(_ name: String) {
        self.name = name
    }
}

class MyClass {
    var user: Box<User>

    init() {
        self.user = Box.Empty
    }

    func showUser(u: User) {
        // do something
    }

    func handleError(err: NSError) {
        // handle your error
    }

    func tryShowUser() {
        switch user {
        case Box.Full(let wrappedUser):
            showUser(wrappedUser.value) // == wrappedUser.v
        case Box.Failure(let err):
            handleError(err)
        case Box.Empty:
            break
        }
    }
}

Latest podspec

{
    "name": "SwiftBox",
    "version": "0.1.2",
    "summary": "A box implementation for Swift",
    "description": "                   A 
(http://exploring.liftweb.net/master/index-C.html) implementation for Swift.n Helps to handle optional values that can contain errors.n", "homepage": "https://github.com/claudetech/swift-box", "license": { "type": "MIT", "file": "LICENSE" }, "authors": { "Claude Tech": "[email protected]" }, "platforms": { "ios": "8.0", "osx": "10.9" }, "source": { "git": "https://github.com/claudetech/swift-box.git", "tag": "v0.1.2" }, "source_files": "box/*.swift", "frameworks": "Foundation", "requires_arc": true }

Pin It on Pinterest

Share This