PeakResult is a Swift microframework providing a simple Result
type. It is part of the Peak Framework.
The Result type is very simple: it represents either success, with an associated value representing the successful result; or a failure, with an associated error. This is perfect to represent operations which can potentially fail.
// Both are Result<String> even though one has a string and the other contains an error
let niceResult = Result { return "Hello!" }
let throwingResult = Result { throw TestError.justATest }
func throwingMethod() -> String throws { ... }
let result = Result { throwingMethod() }
switch result {
case .success(let value):
print(value) // value is String
case .failure(let error):
print(error.localizedDescription) // handle any error
}
func throwingMethod() -> String throws { ... }
let result = Result { throwingMethod() }
do {
let value = try result.resolve() // value is String
} catch {
print(error.localizedDescription) // handle any error
}
Please see the included tests for further examples.
- Using Cocoapods, add
pod 'PeakResult'
to your Podfile. import PeakResult
where necessary.
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
We use SemVer for versioning.
This project is licensed under the MIT License - see the LICENSE.md file for details
- Inspired by Asynchronous error handling by Olivier Halligon
The Peak Framework is a collection of open-source microframeworks created by the team at 3Squared, named for the Peak District. It is made up of:
Name | Description |
---|---|
PeakCoreData | Provides enhances and conveniences to Core Data . |
PeakOperation | Provides enhancement and conveniences to Operation , making use of the Result type. |
PeakNetwork | A networking framework built on top of Session using PeakOperation, leveraging the power of Codable . |