[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"project-7138":3},{"id":4,"name":5,"fullName":6,"owner":7,"repo":5,"description":8,"homepage":9,"htmlUrl":9,"language":10,"languages":9,"totalLinesOfCode":9,"stars":11,"forks":12,"watchers":13,"openIssues":14,"contributorsCount":15,"subscribersCount":15,"size":15,"stars1d":15,"stars7d":15,"stars30d":15,"stars90d":15,"forks30d":15,"starsTrendScore":15,"compositeScore":16,"rankGlobal":9,"rankLanguage":9,"license":17,"archived":18,"fork":18,"defaultBranch":19,"hasWiki":20,"hasPages":18,"topics":21,"createdAt":9,"pushedAt":9,"updatedAt":22,"readmeContent":23,"aiSummary":24,"trendingCount":15,"starSnapshotCount":15,"syncStatus":25,"lastSyncTime":26,"discoverSource":27},7138,"Result","antitypical\u002FResult","antitypical","Swift type modelling the success\u002Ffailure of arbitrary operations.",null,"Swift",2497,228,35,9,0,59.08,"MIT License",false,"master",true,[],"2026-06-12 04:00:32","# Result\n\n[![Build Status](https:\u002F\u002Ftravis-ci.org\u002Fantitypical\u002FResult.svg?branch=master)](https:\u002F\u002Ftravis-ci.org\u002Fantitypical\u002FResult)\n[![Carthage compatible](https:\u002F\u002Fimg.shields.io\u002Fbadge\u002FCarthage-compatible-4BC51D.svg?style=flat)](https:\u002F\u002Fgithub.com\u002FCarthage\u002FCarthage)\n[![CocoaPods](https:\u002F\u002Fimg.shields.io\u002Fcocoapods\u002Fv\u002FResult.svg)](https:\u002F\u002Fcocoapods.org\u002F)\n[![Reference Status](https:\u002F\u002Fwww.versioneye.com\u002Fobjective-c\u002Fresult\u002Freference_badge.svg?style=flat)](https:\u002F\u002Fwww.versioneye.com\u002Fobjective-c\u002Fresult\u002Freferences)\n\nThis is a Swift µframework providing `Result\u003CValue, Error>`.\n\n`Result\u003CValue, 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.\n\nUsing this µframework instead of rolling your own `Result` type allows you to easily interface with other frameworks that also use `Result`.\n\n## Use\n\nUse `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`.\n\n```swift\ntypealias JSONObject = [String: Any]\n\nenum JSONError: Error {\n    case noSuchKey(String)\n    case typeMismatch\n}\n\nfunc stringForKey(json: JSONObject, key: String) -> Result\u003CString, JSONError> {\n    guard let value = json[key] else {\n        return .failure(.noSuchKey(key))\n    }\n    \n    guard let value = value as? String else {\n        return .failure(.typeMismatch)\n    }\n\n    return .success(value)\n}\n```\n\nThis 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.\n\nOne simple way to handle a `Result` is to deconstruct it using a `switch` statement.\n\n```swift\nswitch stringForKey(json, key: \"email\") {\n\ncase let .success(email):\n    print(\"The email is \\(email)\")\n    \ncase let .failure(.noSuchKey(key)):\n    print(\"\\(key) is not a valid key\")\n    \ncase .failure(.typeMismatch):\n    print(\"Didn't have the right type\")\n}\n```\n\nUsing 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.\n\nOther methods available for processing `Result` are detailed in the [API documentation](http:\u002F\u002Fcocoadocs.org\u002Fdocsets\u002FResult\u002F).\n\n## Result vs. Throws\n\nSwift 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`.\n\nSince 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`.\n\n## Higher Order Functions\n\n`map` and `flatMap` operate the same as `Optional.map` and `Optional.flatMap` except they apply to `Result`.\n\n`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`.\n\n```swift\n\u002F\u002F transforms a Result\u003CInt, JSONError> to a Result\u003CString, JSONError>\nlet idResult = intForKey(json, key:\"id\").map { id in String(id) }\n```\n\nHere, the final result is either the id as a `String`, or carries over the `failure` from the previous result.\n\n`flatMap` is similar to `map` in that it transforms the `Result` into another `Result`. However, the function passed into `flatMap` must return a `Result`.\n\nAn 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](http:\u002F\u002Fwww.javiersoto.me\u002Fpost\u002F106875422394).\n\n## Integration\n\n### Carthage\n\n1. Add this repository as a submodule and\u002For [add it to your Cartfile](https:\u002F\u002Fgithub.com\u002FCarthage\u002FCarthage\u002Fblob\u002Fmaster\u002FDocumentation\u002FArtifacts.md#cartfile) if you’re using [carthage](https:\u002F\u002Fgithub.com\u002FCarthage\u002FCarthage\u002F) to manage your dependencies.\n2. Drag `Result.xcodeproj` into your project or workspace.\n3. Link your target against `Result.framework`.\n4. 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.)\n\n### Cocoapods\n\n```ruby\npod 'Result', '~> 5.0'\n```\n\n### Swift Package Manager\n\n```swift\n\u002F\u002F swift-tools-version:4.0\nimport PackageDescription\n\nlet package = Package(\n    name: \"MyProject\",\n    targets: [],\n    dependencies: [\n        .package(url: \"https:\u002F\u002Fgithub.com\u002Fantitypical\u002FResult.git\",\n                 from: \"5.0.0\")\n    ]\n)\n```\n","Result 是一个用于建模任意操作成功或失败状态的 Swift 微框架。它提供了一个 `Result\u003CValue, Error>` 类型，可以表示操作的结果是成功（包含 `Value`）还是失败（包含 `Error`），类似于 Swift 的原生 `Optional` 类型，但增加了错误处理的能力。该框架特别适用于需要处理可能失败的操作的场景，例如从 JSON 数据中提取特定键值时，能够更清晰地表达错误信息，并且易于与其他使用相同 `Result` 类型的框架进行集成。通过使用 `switch` 语句，开发者可以方便地对 `Result` 进行模式匹配，确保所有可能的情况都被妥善处理。",2,"2026-06-11 03:10:45","top_language"]