Answer the question
In order to leave comments, you need to log in
How to bind Alamofire+ObjectMapper+RxSwift similar to Retrofit+Gson+RxJava in Android?
Good afternoon, I recently started studying development for iOS, before that I had experience with Android. Android has such wonderful tools as:
* Retrofit + RxJava adapter - Working with HTTP
* Gson - Mapping JSON to Java objects
* RxJava - reactive language extensions
All this in one bundle allows you to create an interface ("protocol" in terms of Swift) like this:
interface MyService {
@GET("/user")
Observable<User> getUser();
}
GET http://mydomain.com/user
), mapping and creating an Observable takes place under the hood and these things do not interfere with further development. class MyService {
func getUser() -> Observable<User> {
// Запускаем асинхронный HTTP-запрос, маппим и возвращаем Observable. Как?
}
}
{"id":1,"name":"Jack"}
struct User: ImmutableMappable {
let id: Int
let name: String
/// Маппим
init(map: Map) throws {
id = try map.value("id")
name = try map.value("name")
}
}
class MyService {
func getUser() -> Observable<User> {
return RxAlamofire.json(
.get,
"http://mydomain.com/user"
).map { json -> User in
return try Mapper<User>().map(JSONObject: json)
}
}
}
Answer the question
In order to leave comments, you need to log in
Thanks for the code, maybe you optimized it in a year, can you tell?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question