Moya映射器无法从result中映射对象 - Swift 3

时间:2017-08-01 06:23:30

标签: swift3 xcode8 moya

嗨,嗨, 我正在使用moyamapper我尝试过不同的库,但我发现所有这些都存在同样的问题 我无法理解我哪里出错了。 现在我有一个方法可以得到一个moya结果的结果,直到她

这里我使用的是pod 'MoyaModelMapper'

func getCategories() {
    let APIProviderClosure = APIHelper.getAPIProvider()
    let provider = MoyaProvider<APIType>(endpointClosure:APIProviderClosure, plugins: [NetworkLoggerPlugin(verbose: true)])
provider.request(APIType.categories()) { (result) in
    if case .success(let response) = result {
      do {

        let repos1 = try response.map(to: [activities.self],fromKey: "data.activities")   //<---ERROR HERE
        let repos = try response.mapString(atKeyPath: "status")   
        Logger.log(repos)
        Logger.log(repos1)
      } catch MoyaError.jsonMapping(let error) {
        Logger.log(msgTitle: "ERROR-->", msg: error)
      } catch {
        print(":(")
      }
    }
  }

}

我在这里得到的结果很好 但是当我尝试将结果映射到一个对象时,我得到一个错误,如果我将字符串映射为一个简单的状态字符串,那么它可以工作,但是它会抛出错误,而且我无法理解这里的错误。

我试过

Moya-ModelMapper

和许多其他映射器库,但我得到相同的问题。 下面是我试图解析为对象的json

{

"status":"success",
"message":[
],
"code":200,
"data":{
    "activities":[
        {
            "sub_categories":[
                {
                    "is_project":true,
                    "id":11,
                    "title":"Timelog"
                },
                {
                    "is_project":true,
                    "id":14,
                    "title":"Magikkart"
                }
            ],
            "id":45,
            "title":"Client Call / Chat"
        },
        {
            "sub_categories":[
            ],
            "id":55,
            "title":"Code Review"
        },
        {
            "sub_categories":[
                {
                    "is_project":true,
                    "id":11,
                    "title":"Timelog"
                },
                {
                    "is_project":true,
                    "id":14,
                    "title":"Magikkart"
                }
            ],
            "id":56,
            "title":"Designing"
        },
        {
            "sub_categories":[
                {
                    "is_project":true,
                    "id":11,
                    "title":"Timelog"
                },
                {
                    "is_project":true,
                    "id":14,
                    "title":"Magikkart"
                }
            ],
            "id":50,
            "title":"Development"
        },
        {
            "sub_categories":[
            ],
            "id":1,
            "title":"FAN Session"
        },
        {
            "sub_categories":[
            ],
            "id":3,
            "title":"Interview"
        },
        {
            "sub_categories":[
            ],
            "id":40,
            "title":"Lunch"
        },
        {
            "sub_categories":[
                {
                    "is_project":true,
                    "id":11,
                    "title":"Timelog"
                },
                {
                    "is_project":true,
                    "id":14,
                    "title":"Magikkart"
                }
            ],
            "id":7,
            "title":"Meeting"
        },
        {
            "sub_categories":[
            ],
            "id":6,
            "title":"Miscellaneous"
        },
        {
            "sub_categories":[
            ],
            "id":41,
            "title":"Project Review"
        },
        {
            "sub_categories":[
            ],
            "id":52,
            "title":"Sales Follow-up"
        },
        {
            "sub_categories":[
            ],
            "id":2,
            "title":"Scrum Meeting"
        },
        {
            "sub_categories":[
            ],
            "id":4,
            "title":"Seminar"
        },
        {
            "sub_categories":[
            ],
            "id":43,
            "title":"Study / Self Learning"
        },
        {
            "sub_categories":[
            ],
            "id":5,
            "title":"Tea Break"
        }
    ]
}

}

我要解析的模型是

class Activities: Mappable {
  var id:String?
  var title:String?
  var isProject:String?
  var subCategories:[Activities]?

  //Default Init
  init(){}

  public required init(map: Mapper) throws {
    try id = map.from("id")
    try title = map.from("title")
    try isProject = map.from("is_project")
    try subCategories = map.from("sub_categories")
  }

  func hasSubCategories() -> Bool {
    if((self.subCategories?.count)! > 0){
      return true
    }else{
      return false
    }
  }

  func getSubCategories() -> [Activities] {
    return self.subCategories!
  }
 }

提前致谢!!!

0 个答案:

没有答案