Moya mapper not able to mapObjects from result - Swift 3

人走茶凉 提交于 2019-12-24 10:41:15

问题


Well Hi, I am using moyamapper I've tried different libraries but I am finding the same issue with all of them I am not able to understand where I am going wrong. Now I have a method which get the result in a moya result and Its fine till her

here I am using 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(":(")
      }
    }
  }

}

I am getting the result here and its fine but when I try to map the result to an object I an getting an error and If I map string a simple status string then it works but with objects it throws Error and I am not able to understand whats wrong here.

I've tried

Moya-ModelMapper

and many other mapper libraries but I get the same issue. Below is the json I am trying to parse in to object

{

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

}

And the model to which I want to parse it to is

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!
  }
 }

Thanks in Advance!!!

来源:https://stackoverflow.com/questions/45430329/moya-mapper-not-able-to-mapobjects-from-result-swift-3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!