Hi, I want to remove FatalErrors from my code, I tried in different ways, but I think I'm doing it wrong. This is my old code:
import Foundation
import UIKit
import RxSwift
import RxCocoa
class Service {
func graphQL(body: [String: Any?]) -> Observable<Foundation.Data> {
guard let urlValue = Bundle.main.urlValue else { fatalError("Error with info.plist") }
let request = urlRequest(urlValue: urlValue, body: body)
return URLSession.shared.rx.data(request: request)
}
private func urlRequest(urlValue: String, body: [String: Any?]) -> URLRequest {
guard let url = URL(string: urlValue) else { fatalError("Error with urlValue") }
var request = URLRequest(url: url)
let userKey = Bundle.main.userKeyValue
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue(userKey, forHTTPHeaderField: "userid")
request.httpBody = try? JSONSerialization.data(withJSONObject: body, options: .fragmentsAllowed)
return request
}
}
Now, Im trying to do something like this:
import Foundation
import UIKit
import RxSwift
import RxCocoa
class Service {
func graphQL(body: [String: Any?]) -> Observable<Foundation.Data> {
guard let urlValue = Bundle.main.urlValue else { return nil }
guard let request = urlRequest(urlValue: urlValue, body: body) else { return nil }
return URLSession.shared.rx.data(request: request)
}
private func urlRequest(urlValue: String, body: [String: Any?]) -> URLRequest? {
guard let url = URL(string: urlValue) else { return nil }
var request = URLRequest(url: url)
let userKey = Bundle.main.userKeyValue
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue(userKey, forHTTPHeaderField: "userid")
request.httpBody = try? JSONSerialization.data(withJSONObject: body, options: .fragmentsAllowed)
return request
}
}
but I get errors: 'nil' is incompatible with return type 'Observable <Data>', and if I add to Observable <Foundation.Data>? as optional, then I get errors in my serviceManager.