Skip to content

Instantly share code, notes, and snippets.

@GeekTree0101
Created April 25, 2019 12:54
Show Gist options
  • Save GeekTree0101/9e55ecda04e7190102fbfaa76392fcb3 to your computer and use it in GitHub Desktop.
Save GeekTree0101/9e55ecda04e7190102fbfaa76392fcb3 to your computer and use it in GitHub Desktop.
Easy Network Data -> Model
import RxAlamofire
import Codextended
struct Model: Codable {
var id: Int
init(from decoder: Decoder) throws {
self.id = try! decoder.decode("id")
}
func encode(to encoder: Encoder) throws {
try encoder.encode(self.id, for: "id")
}
}
_ = RxAlamofire.requestData(.get, "https://api.github.com/repositories")
.asObservable()
.observeOn(MainScheduler.instance)
.map({ res -> [Model] in
let (_, data) = res
return try! data.decoded() ?? []
})
.map({ $0.first })
.subscribe(onNext: { model in
print(model?.id)
let data = try? model?.encoded()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment