You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> As this example demonstrates, `startWith` can be chained on a last-in-first-out basis, i.e., each successive `startWith`'s elements will be prepended before the prior `startWith`'s elements.
> Because the `combineLatest` variant that takes a collection passes an array of values to the selector function, it requires that all source `Observable` sequences are of the same type.
Merges two observable sequences into one observable sequence by combining each element from the first source with the latest element from the second source, if any.
Creates an empty `Observable` sequence that only emits a Completed event. [More info](http://reactivex.io/documentation/operators/empty-never-throw.html)
32
32
*/
33
33
example("empty"){
34
-
letdisposeBag=DisposeBag()
35
-
34
+
vardisposeBag=DisposeBag()
35
+
36
36
Observable<Int>.empty()
37
37
.subscribe{ event in
38
38
print(event)
39
39
}
40
-
.disposed(by: disposeBag)
40
+
.disposed(by:&disposeBag)
41
41
}
42
42
/*:
43
43
> This example also introduces chaining together creating and subscribing to an `Observable` sequence.
@@ -46,27 +46,27 @@ example("empty") {
46
46
Creates an `Observable` sequence with a single element. [More info](http://reactivex.io/documentation/operators/just.html)
47
47
*/
48
48
example("just"){
49
-
letdisposeBag=DisposeBag()
50
-
49
+
vardisposeBag=DisposeBag()
50
+
51
51
Observable.just("🔴")
52
52
.subscribe{ event in
53
53
print(event)
54
54
}
55
-
.disposed(by: disposeBag)
55
+
.disposed(by:&disposeBag)
56
56
}
57
57
/*:
58
58
----
59
59
## of
60
60
Creates an `Observable` sequence with a fixed number of elements.
61
61
*/
62
62
example("of"){
63
-
letdisposeBag=DisposeBag()
64
-
63
+
vardisposeBag=DisposeBag()
64
+
65
65
Observable.of("🐶","🐱","🐭","🐹")
66
66
.subscribe(onNext:{ element in
67
67
print(element)
68
68
})
69
-
.disposed(by: disposeBag)
69
+
.disposed(by:&disposeBag)
70
70
}
71
71
/*:
72
72
> This example also introduces using the `subscribe(onNext:)` convenience method. Unlike `subscribe(_:)`, which subscribes an _event_ handler for all event types (Next, Error, and Completed), `subscribe(onNext:)` subscribes an _element_ handler that will ignore Error and Completed events and only produce Next event elements. There are also `subscribe(onError:)` and `subscribe(onCompleted:)` convenience methods, should you only want to subscribe to those event types. And there is a `subscribe(onNext:onError:onCompleted:onDisposed:)` method, which allows you to react to one or more event types and when the subscription is terminated for any reason, or disposed, in a single call:
@@ -83,11 +83,11 @@ example("of") {
83
83
Creates an `Observable` sequence from a `Sequence`, such as an `Array`, `Dictionary`, or `Set`.
84
84
*/
85
85
example("from"){
86
-
letdisposeBag=DisposeBag()
87
-
86
+
vardisposeBag=DisposeBag()
87
+
88
88
Observable.from(["🐶","🐱","🐭","🐹"])
89
89
.subscribe(onNext:{print($0)})
90
-
.disposed(by: disposeBag)
90
+
.disposed(by:&disposeBag)
91
91
}
92
92
/*:
93
93
> This example also demonstrates using the default argument name `$0` instead of explicitly naming the argument.
@@ -96,8 +96,8 @@ example("from") {
96
96
Creates a custom `Observable` sequence. [More info](http://reactivex.io/documentation/operators/create.html)
97
97
*/
98
98
example("create"){
99
-
letdisposeBag=DisposeBag()
100
-
99
+
vardisposeBag=DisposeBag()
100
+
101
101
letmyJust={(element:String)->Observable<String>in
102
102
returnObservable.create{ observer in
103
103
observer.on(.next(element))
@@ -108,32 +108,32 @@ example("create") {
108
108
109
109
myJust("🔴")
110
110
.subscribe{print($0)}
111
-
.disposed(by: disposeBag)
111
+
.disposed(by:&disposeBag)
112
112
}
113
113
/*:
114
114
----
115
115
## range
116
116
Creates an `Observable` sequence that emits a range of sequential integers and then terminates. [More info](http://reactivex.io/documentation/operators/range.html)
117
117
*/
118
118
example("range"){
119
-
letdisposeBag=DisposeBag()
120
-
119
+
vardisposeBag=DisposeBag()
120
+
121
121
Observable.range(start:1, count:10)
122
122
.subscribe{print($0)}
123
-
.disposed(by: disposeBag)
123
+
.disposed(by:&disposeBag)
124
124
}
125
125
/*:
126
126
----
127
127
## repeatElement
128
128
Creates an `Observable` sequence that emits the given element indefinitely. [More info](http://reactivex.io/documentation/operators/repeat.html)
129
129
*/
130
130
example("repeatElement"){
131
-
letdisposeBag=DisposeBag()
132
-
131
+
vardisposeBag=DisposeBag()
132
+
133
133
Observable.repeatElement("🔴")
134
134
.take(3)
135
135
.subscribe(onNext:{print($0)})
136
-
.disposed(by: disposeBag)
136
+
.disposed(by:&disposeBag)
137
137
}
138
138
/*:
139
139
> This example also introduces using the `take` operator to return a specified number of elements from the start of a sequence.
@@ -142,23 +142,23 @@ example("repeatElement") {
142
142
Creates an `Observable` sequence that generates values for as long as the provided condition evaluates to `true`.
143
143
*/
144
144
example("generate"){
145
-
letdisposeBag=DisposeBag()
146
-
145
+
vardisposeBag=DisposeBag()
146
+
147
147
Observable.generate(
148
148
initialState:0,
149
149
condition:{ $0 <3},
150
150
iterate:{ $0 +1}
151
151
)
152
152
.subscribe(onNext:{print($0)})
153
-
.disposed(by: disposeBag)
153
+
.disposed(by:&disposeBag)
154
154
}
155
155
/*:
156
156
----
157
157
## deferred
158
158
Creates a new `Observable` sequence for each subscriber. [More info](http://reactivex.io/documentation/operators/defer.html)
159
159
*/
160
160
example("deferred"){
161
-
letdisposeBag=DisposeBag()
161
+
vardisposeBag=DisposeBag()
162
162
varcount=1
163
163
164
164
letdeferredSequence=Observable<String>.deferred{
@@ -176,36 +176,36 @@ example("deferred") {
176
176
177
177
deferredSequence
178
178
.subscribe(onNext:{print($0)})
179
-
.disposed(by: disposeBag)
180
-
179
+
.disposed(by:&disposeBag)
180
+
181
181
deferredSequence
182
182
.subscribe(onNext:{print($0)})
183
-
.disposed(by: disposeBag)
183
+
.disposed(by:&disposeBag)
184
184
}
185
185
/*:
186
186
----
187
187
## error
188
188
Creates an `Observable` sequence that emits no items and immediately terminates with an error.
189
189
*/
190
190
example("error"){
191
-
letdisposeBag=DisposeBag()
192
-
191
+
vardisposeBag=DisposeBag()
192
+
193
193
Observable<Int>.error(TestError.test)
194
194
.subscribe{print($0)}
195
-
.disposed(by: disposeBag)
195
+
.disposed(by:&disposeBag)
196
196
}
197
197
/*:
198
198
----
199
199
## doOn
200
200
Invokes a side-effect action for each emitted event and returns (passes through) the original event. [More info](http://reactivex.io/documentation/operators/do.html)
//: > There are also `doOnNext(_:)`, `doOnError(_:)`, and `doOnCompleted(_:)` convenience methods to intercept those specific events, and `doOn(onNext:onError:onCompleted:)` to intercept one or more events in a single call.
0 commit comments