[英]How do I print subscribed dictinary value in RxSwift?
I have an Observable
that returns a dictionary
:我有一个返回
dictionary
的Observable
:
func myFunc() -> Observable<[String : MyClass]> {
return service.getDictionary()
}
How do I get ahold of the keys
and values
?我如何获得
keys
和values
?
viewModel.myFunc().subscribe(onNext: {
print("🐱", $0 )
}).disposed(by: disposeBag)
There's no property like .key
or .value
.没有像
.key
或.value
这样的属性。 Do I use map
or flatMap
or how does it work?我是否使用
map
或flatMap
或它是如何工作的?
RxSwift 5.0 Tested RxSwift 5.0 测试
func myFunc() -> Observable<[String : String]> {
let dict:[String:String] = ["key0": "value0", "key1": "value1", "key2": "value2","key3" : "value3"]
return Observable.just(dict)
}
myFunc()
.map { (dict) -> [(String,String)] in
let keys = Array(dict.keys)
var key_value_array = Array<(String,String)>()
for key in keys {
let key_value_tuple = (key,dict[key]!)
key_value_array.append(key_value_tuple)
}
return key_value_array
}.flatMap { Observable.from($0) }
.subscribe(onNext: { (tuple) in
print("Key is \(tuple.0)")
print("Value is \(tuple.1)")
}).disposed(by: disposeBag)
Then log in console is然后登录控制台是
Key is key0
Value is value0
Key is key1
Value is value1
Key is key3
Value is value3
Key is key2
Value is value2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.