Latest 0.1.2
Homepage https://github.com/joalbright/Switchary
License MIT
Platforms ios 9.0, requires ARC
Authors

Simple library to create an assignment that works like a ternary operator mutated with a switch statement

Version
License
Platform

Usage

To run the example project, clone the repo, and run pod install from the Example directory first.

enum AgeGroup: Int { case Baby, Kid, Teen, Adult }

enum LifeStatus: Int { case Alive, Dead, Zombie }

let life: LifeStatus? = .Zombie

Now that our variables are ready, we can play with the features.

First I want to show you how I wrote a switch assignment before. It was ok, but I don’t like to settle for ok.

// embedded ternary operators ... old way of writing it

let _ = life == .Alive ? UIColor.yellowColor()
      : life == .Dead ? UIColor.redColor()
      : life == .Zombie ? UIColor.grayColor()
      : UIColor.greenColor()

The inline Switchary assignment makes this much more readable.

// Switchary assignment inline

// ??? starts the switch
// ||| seperates the cases
// *** is our default value

let _ = life ??? .Alive --> UIColor.yellowColor()
             ||| .Dead --> UIColor.redColor()
             ||| .Zombie --> UIColor.grayColor()
             *** UIColor.greenColor()
// Switchary Range

let _ = 21 ??? (0...3) --> AgeGroup.Baby
           ||| (4...12) --> AgeGroup.Kid
           ||| (13...19) --> AgeGroup.Teen
           *** AgeGroup.Adult

Currently I only support ranges, enums and basic types for the inline assignment. But I wanted to support all types of pattern matching. This closure assignment allows you to pass a value to match against and returns a value to be assigned.

// Switchary closure

let _ = life ??? {

    switch $0 {

    case .Alive: return UIColor.greenColor()
    case .Dead: return UIColor.redColor()
    case .Zombie: return UIColor.grayColor()

    }

}

let _ = 12 ??? {

    switch $0 {

    case 0..<10: return UIColor.clearColor()
    case let x where x < 20: return UIColor.yellowColor()
    case let x where x < 30: return UIColor.orangeColor()
    case let x where x < 40: return UIColor.redColor()
    default: return UIColor.whiteColor()

    }

}

Lastly there is an initializer protocol SwitchInit that takes a value and closure within the init. This allows for simple custom initialization based on the value pased in.

// Switchary Initalizer

extension UIView: SwitchInit { }

let button = UIButton (life) {

    switch $0 {

    case .Alive : $1.setTitle("Eat Lunch", forState: .Normal)
    case .Dead : $1.setTitle("Eat Dirt", forState: .Normal)
    case .Zombie : $1.setTitle("Eat Brains", forState: .Normal)

    }

}   

TBH : I have not found a good use for this feature yet.

Installation

Switchary is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "Switchary"

Switchary is also available through Swift Package Manager. Please take a look at the link to learn more about how to use SwiftPM.

import PackageDescription

let package = Package(
    name: "YOUR_PACKAGE_NAME",
    dependencies: [
        .Package(url: "https://github.com/joalbright/Switchary.git", majorVersion: 0)
    ]
)

Author

Jo Albright

License

Switchary is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "Switchary",
    "version": "0.1.2",
    "summary": "Simple library to create an assignment that works like a ternary switch",
    "description": "The library supports a few operators to build a ternary - switch assignment. Also, has built in closure assignment.",
    "homepage": "https://github.com/joalbright/Switchary",
    "license": "MIT",
    "authors": {
        "Jo Albright": "[email protected]"
    },
    "source": {
        "git": "https://github.com/joalbright/Switchary.git",
        "tag": "0.1.2"
    },
    "social_media_url": "https://twitter.com/joalbright",
    "platforms": {
        "ios": "9.0"
    },
    "requires_arc": true,
    "source_files": "Sources/**/*",
    "resource_bundles": {
        "Switchary": [
            "Pod/Assets/*.png"
        ]
    }
}

Pin It on Pinterest

Share This