title: How can I get the value from a service that returns an Observable using RxSwift
body: <p>I am trying to understand <code>RxSwift</code>. I have created a simple playground below that mocks out the behaviour I am attempting. This should run OK.</p>
  
  <pre><code>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&lt;T: Codable&gt; {
  
      private let baseURL = URL(string: "http://universities.hipolabs.com/")!
  
      func call() -&gt; Observable&lt;T&gt; {
          return Observable&lt;T&gt;.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&lt;Universities&gt;()
      private let disposeBag = DisposeBag()
  
  
  
      func getValues() {
          client.call().subscribe(onNext: { print($0.example) }).disposed(by: disposeBag)
      }
  }
  
  let service = SomeService()
  
  service.getValues()
  </code></pre>
  
  <p>As you can see <code>SomeService</code> calls my http client which makes a request and returns the response. I then print out a value. What I'm struggling to understand is how can I subscribe to <code>getValues</code> in another class, and make use of the response.</p>
  
  <p>This was my current attempt</p>
  
  <pre><code>class SomeService {
      private let client = HTTPClient&lt;Universities&gt;()
      private let disposeBag = DisposeBag()
  
  
  
      func getValues() -&gt; Observable&lt;String&gt; {
          return client.call().subscribe(onNext: { return $0.example }).disposed(by: disposeBag)
      }
  }
  
  let service = SomeService()
  
  service.getValues().map { print($0) }
  </code></pre>
  
  <p>However I get an error </p>
  
  <blockquote>
    <p>Cannot convert return expression of type '()' to return type
    'Observable'</p>
  </blockquote>
  
  <p>Should I be creating some sort of subject and subscribing to that from my controller and instead calling <code>onNext</code> with the returned value?</p>
  
  <p>If so how would I structure those calls in my controller?</p>