AlamofireObjectMapper in swift 3 - json

I want to use AlamofireObjectMapper for the first time to parse a json response in swift.
The response is:
"success": true,
"terms": "https:\/\/currencylayer.com\/terms",
"privacy": "https:\/\/currencylayer.com\/privacy",
"timestamp": 1480007048,
"source": "USD",
"quotes": {
"USDAED": 3.672598,
"USDAFN": 66.599998,
"USDALL": 127.999937,
"USDAMD": 478.679993,
"USDANG": 1.780277,
"USDAOA": 165.072998,
"USDARS": 15.497261,
"USDAUD": 1.348899,
"USDAWG": 1.79,
"USDAZN": 1.714104,
"USDBAM": 1.855297,
"USDBBD": 2,
"USDBDT": 79.179735,
"USDBGN": 1.854199,
"USDBHD": 0.377036,
"USDBIF": 1668.300049,
"USDBMD": 1,
"USDBND": 1.429902,
"USDBOB": 6.870014,
"USDBRL": 3.396898,
"USDBSD": 1,
}
I mapped it like this:
class ModelCurrency: Mappable {
var success : Bool?
var terms : String?
var privacy : String?
var timestamp : CGFloat?
var source : String?
var quotes : [Quotes]?
init() {}
required init?(map: Map) {
}
func mapping(map: Map) {
success<-map["success"]
terms<-map["terms"]
privacy<-map["privacy"]
timestamp<-map["timestamp"]
source<-map["source"]
quotes<-map["quotes"]
print("It json\(terms)")
}
}
class Quotes : Mappable {
var name : String?
var val : CGFloat?
required init?(map: Map) {
}
func mapping(map: Map) {
name<-map["name"]
val<-map["val"]
}
}
And in my controller:
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
super.viewDidLoad()
let URL = "http://www.apilayer.net/api/live?access_key=ad847a0a855c0647590df2b818923025"
Alamofire.request(URL).responseArray(queue: "quotes") { (response: DataResponse<[Quotes]>) in
let arrayCurency = response.result.value!
for quotes in arrayCurency {
print(quotes.name!)
print(quotes.val!)
}
}
}
It gives me error mapping and this error:
cannot convert value 'String' to expected argument type 'DispatchQueue?'

There are several issues.
You want to reference DataResponse<ModelCurrency> rather than DataResponse<[Quotes]>.
You want to use responseObject, not responseArray.
You don't want to use that queue parameter with a String value. The queue parameter is used to specify which dispatch queue you want the completion handlers to run. But that's not what you're doing here, so you should just remove it.
The value associated with the quotes key is not an array of objects. It is yet another dictionary. So you should map it to a dictionary, and then use map method to convert that to an array of Quote objects.
So, pulling that all together:
Alamofire.request(urlString).responseObject { (response: DataResponse<ModelCurrency>) in
...
}
And
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) { }
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) }
}
}
class Quote {
var name : String?
var val : CGFloat?
init(name: String?, val: CGFloat?) {
self.name = name
self.val = val
}
}
(I've renamed Quotes to Quote as it would appear to be a quote for a single currency.)

The issue is with this part of code:
func mapping(map: Map) {
name<-map["name"]
val<-map["val"]
}
in Quotes.
What you should do is map dictionary:
var quotes: [String : CGFloat] = [:]
and when mapping use:
quotes <- map["quotes"]
Take a look at:
https://github.com/Hearst-DD/ObjectMapper
at the basics there is mapping dictionary.
To be more specific, in quote object you don't have name and val JSON objects, it is just what you want. If you map it to dictionary you will be able to access theses values.
Haven't seen your image - but if you don't change above the app will crash.
For the above issue you need to provide queue in which you want to run your request, if you don't want to mess with that and you actually want keyPath, then you need to call function with keyPath: instead of queue:. Link:
https://github.com/tristanhimmelman/AlamofireObjectMapper
look for Array Responses

Related

Swift : The data couldn’t be read because it isn’t in the correct format

I try to call the POST Api with Alamofire but it's showing me an error of incorrect format.
This is my JSON response:
[
{
"_source": {
"nome": "LOTERIAS BELEM",
"endereco": "R DO COMERCIO, 279",
"uf": "AL",
"cidade": "BELEM",
"bairro": "CENTRO"
},
"_id": "010177175"
},
{
"_source": {
"nome": "Bel Loterias"
},
"_id": "80224903"
},
{
"_source": {
"nome": "BELLEZA LOTERIAS",
"endereco": "R RIVADAVIA CORREA, 498",
"uf": "RS",
"cidade": "SANTANA DO LIVRAMENTO",
"bairro": "CENTRO"
},
"_id": "180124986"
}
]
class Album: Codable {
var _source : [_source]
}
class _source: Codable {
var nome : String
var endereco : String
var uf : String
var cidade : String
var bairro : String
}
var arrList = [Album]()
And this is how i try to Decoding with Alamofire.
func request() {
let urlString = URL(string: "My Url")
// Alamofire.request(url!).responseJSON {(response) in
Alamofire.request(urlString!, method: .post, parameters: ["name": "belem"],encoding: JSONEncoding.default, headers: nil).responseJSON {
(response) in
switch (response.result) {
case .success:
if let data = response.data {
do {
let response = try JSONDecoder().decode([Album].self, from: data)
DispatchQueue.main.async {
self.arrList = response
}
}
catch {
print(error.localizedDescription)
}
}
case .failure( let error):
print(error)
}
}
}
Just your Album model is incorrect.
struct Album: Codable {
var source : Source
var id : String
enum CodingKeys: String, CodingKey {
case source = "_source"
case id = "_id"
}
}
struct Source: Codable {
var nome : String
var endereco : String?
var uf : String?
var cidade : String?
var bairro : String?
}
If you don't want _id altogether then simply remove the related parts.
As for your Alamofire related code, that part is good.
Notable improvements:
Have avoided underscored variable name in model by customizing CodingKeys for key mapping purpose
Typenames should always start with a Capital letter (so _source is Source)
Similarly, variable names should always start with a lowercase letter
Made some variables optional (based on your updated response)
Keeping a variable non-optional means it must be present in the response for the model to be created
Making a variable optional means that key may or may not be present in the response and it not being there won't prevent the model from being created
I would like to recommend you to use json4swift.com. You just have to copy your json and paste there. It will automatically create modal struct or class from your json.
Coming back to your question, Your class Album doesn't have array of [_source]. That's the reason you are getting following error "The data couldn’t be read because it isn’t in the correct format".
Try below given format of album class,
class Album: Codable
{
var source: Source?
var id: String?
}
Please try to avoid using underscore in Swift.

Swift - Mapping of Nested Objects (Objectmapper)

I am developing an application with Swift 4. Where I make a call to the APIRest with Alamofire and I want to map the JSON response with Objectmapper. Well, the JSON that calls me back is the following:
The code to the APIRest is:
func retrievePostListData() {
Alamofire
.request("http://www.speedrun.com/api/v1/games", method: .get)
.validate()
.responseArray(completionHandler: { (response:
DataResponse<[PostModelSpeedRunModel]>) in
switch response.result {
case .success(let posts):
self.remoteRequestHandler?.onPostsRetrievedData(posts)
case .failure( _):
self.remoteRequestHandler?.onError()
}
})
}
The problem is that I do not know how to access each of the values (func mapping). Because there are some nested values. In addition to that some of the annunciations are objects and others are array. My erroneous code is the following:
import Foundation
import ObjectMapper
struct PostModelSpeedRunModel {
var id = ""
var international = ""
var abbreviation = ""
var links = [Links]??? // I need to get "rel" and "uri" of "runs"
var uri = ""
}
extension PostModelSpeedRunModel: Mappable {
init?(map: Map) {
}
mutating func mapping(map: Map) {
id <- map["data.id"]
international <- map["data.international"]
abbreviation <- map["data.abbreviation"]
link <- map["data.Links"]
uri <- map["data.logo"]
}
}
Can you help me do / understand doing the function mapping? Thanks
I'm assuming you are getting no values at all, because I tried your code and got nothing. If you only need the contents of the data array from the json and, as you have it now, ObjectMapper is expecting a json with just an array of PostModelSpeedRunModels. Therefore, you need to add a keyPath to tell AlamofireObjectMapper it's starting point:
Alamofire.request("http://www.speedrun.com/api/v1/games", method: .get)
.responseArray(keyPath: "data") { (response: DataResponse<[PostModelSpeedRunModel]>) in
...
}
If you also need the info from the pagination node, then you'll need to create a new object that has a data and pagination properties, and change responseArray(keyPath: ...) to simply responseObject using the new root object in DataResponse.
Then I believe you only want runs's uri, so I recommend just having a String in your model for storing it, instead of an array of Links. Assuming that the array of links is unsorted and may change order in the future (if not you can access directly like map["links.1.uri"] and you are done), all links need to be parsed and then filtered. It can be done as follows:
struct PostModelSpeedRunModel {
var id = ""
var international = ""
var abbreviation = ""
var runsLink = ""
var uri = ""
}
extension PostModelSpeedRunModel: Mappable {
init?(map: Map) {
}
mutating func mapping(map: Map) {
id <- map["id"]
international <- map["international"]
abbreviation <- map["abbreviation"]
uri <- map["logo"]
var links: [Link]?
links <- map["links"]
if let uri = links?.first(where: {$0.rel == "runs"})?.uri {
runsLink = uri
}
}
}
struct Link {
var rel = ""
var uri = ""
}
extension Link: Mappable {
init?(map: Map) {
}
mutating func mapping(map: Map) {
rel <- map["rel"]
uri <- map["uri"]
}
}

How to map different type using ObjectMapper?

I'm using ObjectMapper to map my JSON to Swift object.
I have the following Swift object:
class User: Mappable {
var name: String?
var val: Int?
required init?(map: Map) { }
func mapping(map: Map) {
name <- map["name"]
val <- map["userId"]
}
}
I have this JSON structure:
{
"name": "first",
"userId": "1" // here is `String` type.
},
{
"name": "second",
"userId": 1 // here is `Int` type.
}
After mapping the JSON, the userId of User which name is "first" is null.
How can I map Int/String to Int?
After reading the code of ObjectMapper, I found an easier way to solve the problem, it's to custom the transform.
public class IntTransform: TransformType {
public typealias Object = Int
public typealias JSON = Any?
public init() {}
public func transformFromJSON(_ value: Any?) -> Int? {
var result: Int?
guard let json = value else {
return result
}
if json is Int {
result = (json as! Int)
}
if json is String {
result = Int(json as! String)
}
return result
}
public func transformToJSON(_ value: Int?) -> Any?? {
guard let object = value else {
return nil
}
return String(object)
}
}
then, use the custom transform to the mapping function.
class User: Mappable {
var name: String?
var userId: Int?
required init?(map: Map) { }
func mapping(map: Map) {
name <- map["name"]
userId <- (map["userId"], IntTransform()) // here use the custom transform.
}
}
Hope it can help others who have the same problem. :)
If your API is like this - it is very bad API. What you could do is have two variables instead of one:
class User: Mappable {
var name: String?
var valInt: Int?
var valString: String?
required init?(map: Map) { }
func mapping(map: Map) {
name <- map["name"]
valInt <- map["val"]
valString <- map["val"]
}
}
You can even add function that will get you value like:
// bellow func mapping(map: Map){
func getUserId() -> String {
if(self.valInt != nil) {
return "\(valInt!)"
}
else {
return valString!
}
}
Or, using if let:
func getUserId() -> String {
if let userId = self.valInt {
return "\(userId)"
}
if let userId = valString {
return userId
}
return ""
}
Or using optionals so later on you can use if let userId = object.getUserId()
func getUserId() -> String? {
if(self.valInt != nil) {
return String(valInt)
}
else {
return valString
}
}
You should improve your API. However, if you can't do that, then try this code:
class User: Mappable {
var name: String?
var val: Int?
required init?(map: Map) { }
func mapping(map: Map) {
name <- map["name"]
// 1: Get the value of JSON and store it in a variable of type Any?
var userIdData: Any?
userIdData <- map["userId"]
// 2: Check the value type of the value and convert to Int type
if userIdData is Int {
val = (userIdData as! Int)
} else if userIdData is String {
val = Int(userIdData as! String)
}
}
}
You can refer to this document: Type Casting

Typecasting a GenericType that conforms with a Protocol

I'm trying to use an operator to parse a JSON into an object that conforms with the protocol mappable.
First I have a class called Map that is which it's function is store the current key it's being parsed and the complete JSON to parse and the current value to be stored
private class Map {
var json: [String : Any]?
var key: String!
convenience init(json: [String : Any]?) {
self.init()
self.json = json
}
public var currentValue: Any? {
get{
guard let key = key, let json = json else {
return nil
}
return json[key]
}
}
public subscript(key: String) -> Map {
// save key and value associated to it
self.key = key
return self
}
}
Then I have an operator that get the current value on the rhs based on the Map.Key and passes it to the lhs, and here lies the problem if the lhs is an Array with an object that follows the protocol Mappable, it should use the the operator recursively, until end parsing.
The problem is: the comparison lhs is Array always returns false, so I can't typecast it to do the necessary iteration to parse it. (commented lines).
Any ideas how to approach the problem?
infix operator <<< // JSON to object
private func <<< <T: Any>(lhs: inout T, rhs: Map) {
if let rhsArray = rhs.currentValue as? Array<Dictionary<String, Any>>{
print(type(of: lhs)) // <Array<(Book in *someHash*)>>
debugPrint(lhs is Array<Mappable>) // false
var rhsReturnArray: Array<Mappable> = []
for currentRHSValue in rhsArray {
// let object = T.Element.self.from(Map(json: currentRHSValue))
// rhsReturnArray.append(object)
}
// Do something
}
else if let value = rhs.currentValue as? T{
lhs = value
}
}
private class Autor: Mappable {
var books: [Book]?
func from(map: Map) -> Bool {
books <<< map["Books"]
return true
}
}
private class Book: Mappable {
var title: String?
var numberOfPages: Int?
func from(map: Map) -> Bool {
title <<< map["Title"]
numberOfPages <<< map["Number of Pages"]
return true
}
}
let someDictionary = ["Books" : [["Title": "Happy Potter", "Number of Pages": 420], ["Title": "Happy Potter2", "Number of Pages": 422]]]
private let autor = Autor()
autor.from(map: Map(json: someDictionary))
print(autor.books?.first)
And I can't use third part frameworks on this project.

How to map a realm list of custom objects using Mappable protocol in Swift

In my Realm object model I have an object called "Event". Each Event has a list of EventLocatons. I am trying to map these object from json, but the list of EventLocations is always empty.
The objects look like this (simplified for clarity):
class Event: Object, Mappable {
override class func primaryKey() -> String? {
return "id"
}
dynamic var id = ""
var eventLocations:List<EventLocation> = List<EventLocation>()
func mapping(map: Map) {
id <- map["id"]
eventLocations <- map["eventLocations"]
}
}
class EventLocation: Object, Mappable {
override class func primaryKey() -> String? {
return "id"
}
dynamic var id: String = ""
dynamic var name: String = ""
required convenience init?(_ map: Map) {
self.init()
}
func mapping(map: Map) {
id <- map["id"]
name <- map["name"]
}
}
The json, that I have is an array of Event objects. It comes from an Alamofire response and I map it like that:
var events = Mapper<Event>().mapArray(json!)
The json looks like this:
[
{
"id" : "21dedd6d",
"eventLocations" : [
{
"name" : "hh",
"id" : "e18df48a",
},
{
"name" : "tt",
"fileId" : "be6116e",
}
]
},
{
"id" : "e694c885",
"eventLocations" : [
{
"name" : "hh",
"id" : "e18df48a",
},
{
"name" : "tt",
"fileId" : "be6116e",
}
]
}
]
Does anyone know how can I map a list of custom objects using the Mappable protocol. Whay is the "eventLocations" list always empty?
Having a look at one of the Issues page on ObjectMapper's GitHub repo, it doesn't look like Realm List objects are properly supported yet.
That issue also lists a potential workaround for getting it to work for the time being, which I'll mirror here:
class MyObject: Object, Mappable {
let tags = List<Tag>()
required convenience init?(_ map: Map) { self.init() }
func mapping(map: Map) {
var tags: [Tag]?
tags <- map["tags"]
if let tags = tags {
for tag in tags {
self.tags.append(tag)
}
}
}
}
Another solution could be to implement a custom transform for ObjectMapper.
You can find an implementation here.
Then in your code:
eventLocations <- (map["eventLocations"], ListTransform<EventLocation>())
You can add an operator for this.
Swift 3 implementation:
import Foundation
import RealmSwift
import ObjectMapper
infix operator <-
/// Object of Realm's List type
public func <- <T: Mappable>(left: List<T>, right: Map) {
var array: [T]?
if right.mappingType == .toJSON {
array = Array(left)
}
array <- right
if right.mappingType == .fromJSON {
if let theArray = array {
left.append(objectsIn: theArray)
}
}
}
Now you don't need any additional code or transforms.
list <- map["name"]
I've created a gist. Please, check https://gist.github.com/danilValeev/ef29630b61eed510ca135034c444a98a