Created
March 11, 2017 02:56
-
-
Save robertmryan/b91cb172dd4cff2bfc3b37766eff2d1b to your computer and use it in GitHub Desktop.
AlamofireObjectMapper Demo from http://stackoverflow.com/a/40792947/1271826
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // 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)") | |
| } | |
| } | |
| } | |
| } | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
successwas nottrue; and (b) set up another wrapper to handle the error.