Skip to content

Instantly share code, notes, and snippets.

@robertmryan
Created March 11, 2017 02:56
Show Gist options
  • Select an option

  • Save robertmryan/b91cb172dd4cff2bfc3b37766eff2d1b to your computer and use it in GitHub Desktop.

Select an option

Save robertmryan/b91cb172dd4cff2bfc3b37766eff2d1b to your computer and use it in GitHub Desktop.
AlamofireObjectMapper Demo from http://stackoverflow.com/a/40792947/1271826
//
// ViewController.swift
// MyApp4
//
// Created by Robert Ryan on 3/10/17.
// Copyright © 2017 Robert Ryan. All rights reserved.
//
import UIKit
import Alamofire
import AlamofireObjectMapper
import ObjectMapper
class CurrencyFailureResponse: Mappable {
var success: Bool?
var message: String?
required init?(map: Map) { }
func mapping(map: Map) {
success <- map["success"]
message <- map["message"]
}
}
extension CurrencyFailureResponse: CustomStringConvertible {
var description: String { return "<CurrencyFailureResponse; success = \(success); message = \(message)>" }
}
class ModelCurrency: Mappable {
var success : Bool?
var terms : String?
var privacy : String?
var timestamp : CGFloat?
var source : String?
var quotes : [Quote]?
required init?(map: Map) {
guard let success = map.JSON["success"] as? Bool, success else { return nil }
}
func mapping(map: Map) {
success <- map["success"]
terms <- map["terms"]
privacy <- map["privacy"]
timestamp <- map["timestamp"]
source <- map["source"]
var dictionary: [String: CGFloat]?
dictionary <- map["quotes"]
quotes = dictionary?.map { return Quote(name: $0.key, val: $0.value) }
}
}
extension ModelCurrency: CustomStringConvertible {
var description: String { return "<Model Currency; success = \(success); terms = \(terms); privacy = \(privacy); timestamp = \(timestamp); source = \(source); quotes = \(quotes)>" }
}
class Quote {
var name : String?
var val : CGFloat?
init(name: String?, val: CGFloat?) {
self.name = name
self.val = val
}
}
extension Quote: CustomStringConvertible {
var description: String { return "<Quote; name = \(name); val = \(val)>" }
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let url = "http://..."
let parameters = ["foo": "baz"]
Alamofire.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default)
.responseObject { (response: DataResponse<ModelCurrency>) in
switch response.result {
case .success(let value):
print("\(value)")
case .failure(let error):
print("\(error)")
}
}
.responseObject { (response: DataResponse<CurrencyFailureResponse>) in
switch response.result {
case .success(let value):
print("\(value)")
case .failure(let error):
print("\(error)")
}
}
}
}
@robertmryan

Copy link
Copy Markdown
Author

In the above snippet, I'm assuming that if successful, it returns JSON like shown here, but upon failure, you get some JSON like:

{"success":false, "message": "some error message here"}

You can (a) make the model object fail mapping if success was not true; and (b) set up another wrapper to handle the error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment