How to add objects of entity class(DTO's value) using “addAttributeMappings” in RestKit?

前端 未结 1 1760
梦如初夏
梦如初夏 2021-01-17 01:14

My json response contains not only strings, but also array of DTO. Object mapping is not happening in the right format.

json response body :

{
  \"         


        
相关标签:
1条回答
  • 2021-01-17 01:36

    The below solved my issue :

    In LoanDetailServiceClass,

    func getLoanDetails(success: @escaping((_ loanDetailModel: LoanDetailModel?) -> ()),
                    failure: @escaping ((_ error:String) -> ())) {
    
        // Define mappings
        let postMapping: RKObjectMapping = RKObjectMapping(for: LoanDetailModel.self)
        postMapping.addAttributeMappings(from: ["id","componentName"])
    
        let dTOListMapping: RKObjectMapping = RKObjectMapping(for: DynamicTableDetailDTOList.self)
        dTOListMapping.addAttributeMappings(from: ["fieldName",
                                                   "fieldType",
                                                   "label"])
        postMapping.addRelationshipMapping(withSourceKeyPath: "DynamicTableDetailDTOList" ,
                                           mapping: dTOListMapping)
    
        // Define response decriptor
        let statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClass.successful)
        let resDescriptor = RKResponseDescriptor(mapping: postMapping, method: RKRequestMethod.GET, pathPattern: nil, keyPath: nil, statusCodes: statusCodes)
    
        // Create object manager
        let url = URL(string: baseURL)
        let jsonPlaceholderManager = RKObjectManager(baseURL: url)
        jsonPlaceholderManager?.addResponseDescriptor(resDescriptor)
        RKObjectManager.setShared(jsonPlaceholderManager)
    
        // Perform GET request
        RKObjectManager.shared().getObjectsAtPath(appendedURL, parameters: nil, success: { (operation, mappingResult) -> Void in
            let loanDetailModel: LoanDetailModel = mappingResult!.firstObject as! LoanDetailModel
            success(loanDetailModel)
    
        }) { (operation, error) -> Void in
            failure((error?.localizedDescription)!)
        }
    }
    
    0 讨论(0)
提交回复
热议问题