Mapping to a Swift Object task showing zero using Alamofire ObjectMapper

I am new to iOS and the Swift development environment. I tried to use Alamofire to pull out JSON and AlamofireObjectMapper to map the extracted JSON collections to my Swift Object.

The problem is that I can get the JSON request through Alamofire, and the count is displayed, but the display part seems to show zero. I missed something. Appreciate the help.

Model class

import UIKit
import CoreLocation
import ObjectMapper

class BranchObjectMapper : Mappable {
    // MARK: Properties
    var id: Int?
    var cityId: Int?
    var areaId: Int?
    var name: String?
    var nameAr: String?
    var branchAddr: String?
    var branchAddr2: String?
    var location: CLLocation?

    required init?(_ map: Map) {
        mapping(map)
    }

    func mapping(map: Map) {
        id     <- map["id"]
        cityId    <- map["cityId"]
        areaId  <- map["areaId"]
        name  <- map["name"]
        nameAr  <- map["nameAr"]
        branchAddr  <- map["branchAddr"]
        branchAddr2  <- map["branchAddr2"]
        location  <- map["location"]
    }

}

Request a part in viewDidLoad()

 Alamofire.request(.GET, UrlEndpoints.branchUrl()).responseArray { (response: Response<[BranchObjectMapper], NSError>) in

    self.branchList = response.result.value!

    print(self.branchList.count) // count is correct

    for branch in self.branchList {      
        print(branch.id) // prints nil
        print(branch.id) // prints nil
    }
}

Thank you in advance

The full JSON answer looks below. We built only the necessary models.

[{ "Id": "16", "areaId": "17", "name": "Al Aqiq", "cityId": 4 "," Zip ":" "," nameAr ":" \u0637\u0631\u064a\u0642\u0627\u0644\u0645 "," branchAddr ":" test "," branchAddr2 ":" test "Latitude": "24.60425", "Longitude": "46.629631", "cityId": "1" }]

+4
2

, ObjectMapper lib. Github ObjectMapper.

, lib:

  • Int
  • Bool
  • Double
  • Float
  • String
  • RawRepresentable ()
  • Array<AnyObject>
  • Dictionary<String, AnyObject>
  • Object<T: Mappable>
  • Array<T: Mappable>
  • Array<Array<T: Mappable>>
  • Set<T: Mappable>
  • Dictionary<String, T: Mappable>
  • Dictionary<String, Array<T:Mappable>>

, , nil.

var location: CLLocation?.

CLLocation, CustomCLLocation : JSON ( Json, )

"location":{
    "long": 43.666,
    "lat": 73.4
}

Swift: "CustomCLLocation", , , CLLocation Json

var latitude: Double?
var longitude: Double?

required init?(_ map: Map) {
mapping(map)

}
func mapping(map: Map) {

   longitude <- map["long"]
   latitude <- map["lat"]
}

"" CLLocation:   var location: CustomCLLocation?

, CLLocation. ( CustomCLLocation):

extension CLLocation {
     public class func createFromCustomCLLocation(custom: CustomCLLocation) -> CLLocation {
         return self.init(custom.latitude,custom.longitude)
     }
}

:

var locationCLL = CLLocation.createFromCustomCLLocation(location) // now is a CLLocation

: Alamofire Request

AlamofireObjectMapper ios 8.0 +

Alamofire.request(.GET, UrlEndpoints.branchUrl()).responseArray { (response: [BranchObjectMapper]?, error : ErrorType?) in
    if(error != nil) {
        print(error)
    }else{
        print("data downloaded")


        if let response = response {
            branchList(response)
            for branch in self.branchList {
                print(branch.id)
                print(branch.name)
            }
        }
    }
}
+8

String Int, , dynamic var.

: dynamic var id : Int?

0

Source: https://habr.com/ru/post/1627260/


All Articles