映射到使用Alamofire ObjectMapper显示nil的Swift对象问题

use*_*433 4 json ios alamofire swift2 objectmapper

我是iOS和Swift开发环境的新手.我试图使用Alamofire来提取JSON和AlamofireObjectMapper,以便将检索到的JSON集合映射回我的Swift对象.

问题是我能够通过Alamofire请求获取JSON并显示计数,但映射部分似乎显示为零.是我错过了什么.感谢帮助.

模型类

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"]
    }

}
Run Code Online (Sandbox Code Playgroud)

请求参加 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
    }
}
Run Code Online (Sandbox Code Playgroud)

提前致谢

完整的JSON响应如下所示.在Model中只构建了需要的东西.

[{"Id":"16","areaId":"17","name":"Al Aqiq","cityId":4","Zip":"","nameAr":"\ u0637\u0631\u064a\u0642\u0624\u0645\u0645 \n,"branchAddr":"test","branchAddr2":"test"纬度":"24.60425","经度":"46.629631","cityId":"1"} ]

Pat*_*onz 8

我想你错过了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
}
Run Code Online (Sandbox Code Playgroud)

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"]
}
Run Code Online (Sandbox Code Playgroud)

现在,您可以映射"假"CLLocation对象:var location:CustomCLLocation?

然后,如果你想要一个真正的CLLocation.只需像这样创建一个Simply Extension(将它添加到CustomCLLocation文件中):

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

使用转换:

var locationCLL = CLLocation.createFromCustomCLLocation(location) // now is a CLLocation
Run Code Online (Sandbox Code Playgroud)

编辑:Alamofire请求

我在我的应用程序上使用最新版本的AlamofireObjectMapper for 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)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)