# Maybe

**Maybe** 是 `Observable` 的另外一个版本。它介于 [Single](/rxswift/rxswift_core/observable/single.md) 和 [Completable](/rxswift/rxswift_core/observable/completable.md) 之间，它要么只能发出一个元素，要么产生一个 `completed` 事件，要么产生一个 `error` 事件。

* 发出一个元素或者一个 `completed` 事件或者一个 `error` 事件
* 不会[共享附加作用](/rxswift/recipes/share_side_effects.md)

如果你遇到那种可能需要发出一个元素，又可能不需要发出时，就可以使用 **Maybe**。

## 如何创建 Maybe

创建 **Maybe** 和创建 **Observable** 非常相似：

```swift
func generateString() -> Maybe<String> {
    return Maybe<String>.create { maybe in
        maybe(.success("RxSwift"))

        // OR

        maybe(.completed)

        // OR

        maybe(.error(error))

        return Disposables.create {}
    }
}
```

之后，你可以这样使用 **Maybe**：

```swift
generateString()
    .subscribe(onSuccess: { element in
        print("Completed with element \(element)")
    }, onError: { error in
        print("Completed with an error \(error.localizedDescription)")
    }, onCompleted: {
        print("Completed with no element")
    })
    .disposed(by: disposeBag)
```

你同样可以对 `Observable` 调用 `.asMaybe()` 方法，将它转换为 **Maybe**。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://3440217568.gitbook.io/rxswift/rxswift_core/observable/maybe.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
