我正在尝试了解RxSwift
。我在下面创建了一个简单的游乐场,用来模拟我正在尝试的行为。这样应该可以。
import UIKit
import RxSwift
struct Universities: Codable {
let author: Author
let example: String
let github: String
enum CodingKeys: String, CodingKey {
case author
case example
case github
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
author = try values.decode(Author.self, forKey: .author)
example = try values.decode(String.self, forKey: .example)
github = try values.decode(String.self, forKey: .github)
}
}
struct Author: Codable {
let name: String
let website: String
enum CodingKeys: String, CodingKey {
case name
case website
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
name = try values.decode(String.self, forKey: .name)
website = try values.decode(String.self, forKey: .website)
}
}
class HTTPClient<T: Codable> {
private let baseURL = URL(string: "http://universities.hipolabs.com/")!
func call() -> Observable<T> {
return Observable<T>.create { observer in
let request = URLRequest(url: self.baseURL)
let task: URLSessionDataTask = URLSession.shared.dataTask(with: request) { data, response, error in
do {
let model: T = try JSONDecoder().decode(T.self, from: data!)
observer.onNext(model)
} catch let error {
observer.onError(error)
}
observer.onCompleted()
}
task.resume()
return Disposables.create {
task.cancel()
}
}
}
}
class SomeService {
private let client = HTTPClient<Universities>()
private let disposeBag = DisposeBag()
func getValues() {
client.call().subscribe(onNext: { print($0.example) }).disposed(by: disposeBag)
}
}
let service = SomeService()
service.getValues()
您可以看到SomeService
呼叫了我的http客户端,该客户端发出请求并返回响应。然后,我打印出一个值。我正在努力理解的是如何在另一个课程中订阅getValues
并利用响应。
这是我目前的尝试
class SomeService {
private let client = HTTPClient<Universities>()
private let disposeBag = DisposeBag()
func getValues() -> Observable<String> {
return client.call().subscribe(onNext: { return $0.example }).disposed(by: disposeBag)
}
}
let service = SomeService()
service.getValues().map { print($0) }
但是我遇到错误
无法将类型'()'的返回表达式转换为返回类型 “可观察”
我应该创建某种主题并从我的控制器订阅该主题,而是使用返回的值调用onNext
吗?
如果是的话,我该如何在控制器中构造这些调用?
答案 0 :(得分:2)
func getValues() -> Observable<String> {
return client.call()
.map { $0.example }
.do(onNext: { print($0) })
}
无需在主题之间引入任何内容。执行onNext将挂接可观察的对象,并添加所需的打印行为。 map会将大学类型转换为示例字符串。然后,您可以从ViewController订阅getValues()
的结果。