Result alternatives and similar libraries
Based on the "Utility" category.
Alternatively, view Result alternatives based on common mentions on social networks and blogs.
-
SwifterSwift
A handy collection of more than 500 native Swift extensions to boost your productivity. -
SwiftGen
The Swift code generator for your assets, storyboards, Localizable.strings, … — Get rid of all String-based APIs! -
SwiftGen-Storyboard
The Swift code generator for your assets, storyboards, Localizable.strings, … — Get rid of all String-based APIs! -
R.swift
Strong typed, autocompleted resources like images, fonts and segues in Swift projects -
swift-protobuf
Plugin and runtime library for using protobuf with Swift -
Dollar
A functional tool-belt for Swift Language similar to Lo-Dash or Underscore.js in Javascript -
DifferenceKit
💻 A fast and flexible O(n) difference algorithm framework for Swift collection. -
EZSwiftExtensions
:smirk: How Swift standard types and classes were supposed to work. -
DeepDiff
🦀Amazingly incredible extraordinary lightning fast diffing in Swift -
Device
Light weight tool for detecting the current device and screen size written in swift. -
SwiftLinkPreview
It makes a preview from an URL, grabbing all the information such as title, relevant texts and images. -
WhatsNew
Showcase new features after an app update similar to Pages, Numbers and Keynote. -
Codextended
Extensions giving Swift's Codable API type inference super powers 🦸♂️🦹♀️ -
SwiftyJSONAccelerator
macOS app to generate Swift 5 code for models from JSON (with Codeable) -
Playbook
📘A library for isolated developing UI components and automatically taking snapshots of them. -
ReadabilityKit
Preview extractor for news, articles and full-texts in Swift -
ObjectiveKit
Swift-friendly API for a set of powerful Objective C runtime functions. -
Compass
:earth_africa: Compass helps you setup a central navigation system for your application -
Bow
🏹 Bow is a cross-platform library for Typed Functional Programming in Swift -
Pythonic.swift
Pythonic tool-belt for Swift – a Swift implementation of selected parts of Python standard library. -
BetterSafariView
A better way to present a SFSafariViewController or start a ASWebAuthenticationSession in SwiftUI. -
SwiftPlantUML
A command-line tool and Swift Package for generating class diagrams powered by PlantUML
Appwrite - The open-source backend cloud platform
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of Result or a related project?
Popular Comparisons
README
Result
This is a Swift µframework providing Result<Value, Error>
.
Result<Value, Error>
values are either successful (wrapping Value
) or failed (wrapping Error
). This is similar to Swift’s native Optional
type: success
is like some
, and failure
is like none
except with an associated Error
value. The addition of an associated Error
allows errors to be passed along for logging or displaying to the user.
Using this µframework instead of rolling your own Result
type allows you to easily interface with other frameworks that also use Result
.
Use
Use Result
whenever an operation has the possibility of failure. Consider the following example of a function that tries to extract a String
for a given key from a JSON Dictionary
.
typealias JSONObject = [String: Any]
enum JSONError: Error {
case noSuchKey(String)
case typeMismatch
}
func stringForKey(json: JSONObject, key: String) -> Result<String, JSONError> {
guard let value = json[key] else {
return .failure(.noSuchKey(key))
}
guard let value = value as? String else {
return .failure(.typeMismatch)
}
return .success(value)
}
This function provides a more robust wrapper around the default subscripting provided by Dictionary
. Rather than return Any?
, it returns a Result
that either contains the String
value for the given key, or an ErrorType
detailing what went wrong.
One simple way to handle a Result
is to deconstruct it using a switch
statement.
switch stringForKey(json, key: "email") {
case let .success(email):
print("The email is \(email)")
case let .failure(.noSuchKey(key)):
print("\(key) is not a valid key")
case .failure(.typeMismatch):
print("Didn't have the right type")
}
Using a switch
statement allows powerful pattern matching, and ensures all possible results are covered. Swift 2.0 offers new ways to deconstruct enums like the if-case
statement, but be wary as such methods do not ensure errors are handled.
Other methods available for processing Result
are detailed in the API documentation.
Result vs. Throws
Swift 2.0 introduces error handling via throwing and catching Error
. Result
accomplishes the same goal by encapsulating the result instead of hijacking control flow. The Result
abstraction enables powerful functionality such as map
and flatMap
, making Result
more composable than throw
.
Since dealing with APIs that throw is common, you can convert such functions into a Result
by using the materialize
method. Conversely, a Result
can be used to throw an error by calling dematerialize
.
Higher Order Functions
map
and flatMap
operate the same as Optional.map
and Optional.flatMap
except they apply to Result
.
map
transforms a Result
into a Result
of a new type. It does this by taking a function that transforms the Value
type into a new value. This transformation is only applied in the case of a success
. In the case of a failure
, the associated error is re-wrapped in the new Result
.
// transforms a Result<Int, JSONError> to a Result<String, JSONError>
let idResult = intForKey(json, key:"id").map { id in String(id) }
Here, the final result is either the id as a String
, or carries over the failure
from the previous result.
flatMap
is similar to map
in that it transforms the Result
into another Result
. However, the function passed into flatMap
must return a Result
.
An in depth discussion of map
and flatMap
is beyond the scope of this documentation. If you would like a deeper understanding, read about functors and monads. This article is a good place to start.
Integration
Carthage
- Add this repository as a submodule and/or add it to your Cartfile if you’re using carthage to manage your dependencies.
- Drag
Result.xcodeproj
into your project or workspace. - Link your target against
Result.framework
. - Application targets should ensure that the framework gets copied into their application bundle. (Framework targets should instead require the application linking them to include Result.)
Cocoapods
pod 'Result', '~> 5.0'
Swift Package Manager
// swift-tools-version:4.0
import PackageDescription
let package = Package(
name: "MyProject",
targets: [],
dependencies: [
.package(url: "https://github.com/antitypical/Result.git",
from: "5.0.0")
]
)