[
]
(https://github.com/Carthage/Carthage)
Swift µframework providing Try<T>.
This library is inspired by the Try
implementation in Scala.
enum FileError: ErrorType {
case FileNotFound
case Unknown
}
func lineCountOfFile(filename: String) throws -> Int {
if exists(filename) {
let file = open(filename)
return file.lineCount
} else {
throw FileError.FileNotFound
}
}
// traditional way with Swift 2.0
do {
let lineCount = try lineCountOfFile("data.text")
// do something
} catch {
// error handling
}
let t = Try(try lineCountOfFile("data.text"))
switch t {
case .Success(let lines): print(lines)
case .Failure(let error): print(error)
}
map
<^>
let t = Try(try lineCountOfFile("data.text")).map { $0 * 5 }
switch t {
case .Success(let lines): print(lines)
case .Failure(let error): print(error)
}
flatMap
>>-
let t = Try(try lineCountOfFile("data.text")).flapMap { Try(try doSomething($0)) }
switch t {
case .Success(let lines): print(lines)
case .Failure(let error): print(error)
}
- Operators
let t = Try(try lineCountOfFile("data.text")) <^> { $0 * 5} >>- { Try(try doSomething($0)) }
switch t {
case .Success(let lines): print(lines)
case .Failure(let error): print(error)
}
- Using Carthage
- Insert
github "nghialv/Try"
to your Cartfile- Run
carthage update
- Using Cocoapods
- Insert
use_frameworks!
to your Podfile- Insert
pod "Try"
to your Podfile- Run
pod install
- Using Submodule
- Swift 2.0 (Xcode 7.0 or later)
- iOS 8.0 or later