Latest 0.1.4
Homepage https://github.com/nitrogenice/Swiftry
License MIT
Platforms ios 8.0, osx 10.9, tvos 9.0, requires ARC
Authors

Swift
Carthage compatible
CocoaPods

Swiftry is Scala’s Try brought to Swift.

Usage

Initialization

let a = 3
let x = Swiftry {
  if a % 2 == 0 {
    return a / 2
  } else {
    throw MyError.error
  }
}
let y = Swiftry { 5 }
let z = Swiftry<String>("abracadabra")
let e = Swiftry(error: MyError.error)

Pattern Matching

let x = Swiftry { ... }
switch x {
case let .success(s):
  ...
case let .failure(e):
  ...
}

=> (orElse) Operator

let x = 15
let y = 5
let z = 0

// both equal Swiftry.success(3)
Swiftry {
  if z == 0 {
    throw MyError.error
  } else {
    return y / z
  }
} => Swiftry {
  x / y
}

Swiftry {
  if z == 0 {
    throw MyError.error
  } else {
    return y / z
  }
}.orElse(Swiftry {
  x / y
})

>>- (flatMap) Operator

let divideTwo: (Int) -> Swiftry<Int> = { x in
  Swiftry {
    if x % 2 == 0 {
      return x / 2
    } else {
      throw MyError.error
    }
  }
}

// both equal Swiftry.success(1)
let swiftryOneOp = Swiftry(8) >>- divideTwo >>- divideTwo >>- divideTwo
let swiftryOneCall = Swiftry(8).flatMap(divideTwo).flatMap(divideTwo).flatMap(divideTwo)
// both equal Swiftry.failure(MyError.myerror)
let swiftryFailureOp = swiftryOneOp >>- divideTwo
let swiftryFailureCall = swiftryOneCall.flatMap(divideTwo)

<^> (map) operator

let f: (Int) -> Int = { return 2 * $0 }
// both equal Swiftry.success(10)
let tOp = Swiftry(5) <^> f
let tCall = Swiftry(5).map(f)

Installation

CocoaPods

Add the following line to your Podfile.

pod 'Swiftry', '~> 0.1.4'

Carthage

Add the following line to your Cartfile.

github "nitrogenice/Swiftry" ~> 0.1.4

License

Swiftry is under an MIT license. See the LICENSE file for more information.

Latest podspec

{
    "name": "Swiftry",
    "version": "0.1.4",
    "summary": "A simple Swift Try framework.",
    "homepage": "https://github.com/nitrogenice/Swiftry",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Hyun Min Choi": "[email protected]"
    },
    "requires_arc": true,
    "source": {
        "git": "https://github.com/nitrogenice/Swiftry.git",
        "tag": "0.1.4",
        "submodules": true
    },
    "source_files": "Sources/*.{h,swift}",
    "platforms": {
        "ios": "8.0",
        "osx": "10.9",
        "tvos": "9.0"
    },
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This