# from

**将其他类型或者数据结构转换为 `Observable`**

![](/files/-MWlwQf-CeiLDm9Ypamq)

当你在使用 `Observable` 时，如果能够直接将其他类型转换为 `Observable`，这将是非常省事的。**from** 操作符就提供了这种功能。

## 演示

将一个**数组**转换为 `Observable`：

```swift
let numbers = Observable.from([0, 1, 2])
```

它相当于：

```swift
let numbers = Observable<Int>.create { observer in
    observer.onNext(0)
    observer.onNext(1)
    observer.onNext(2)
    observer.onCompleted()
    return Disposables.create()
}
```

将一个**可选值**转换为 `Observable`：

```swift
let optional: Int? = 1
let value = Observable.from(optional: optional)
```

它相当于：

```swift
let optional: Int? = 1
let value = Observable<Int>.create { observer in
    if let element = optional {
        observer.onNext(element)
    }
    observer.onCompleted()
    return Disposables.create()
}
```


---

# 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/decision_tree/from.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.
