swift - 如何将 Response 对象映射到 Alamofire 4.0 Post 方法

标签 swift alamofire

嗨,我是 swift 编程语言的新手,我想使用 Alamofire 4.0 POST 方法映射响应对象,为此我在下面编写了代码,但它显示了像 ObjectMapiing 失败这样的异常,请帮助我

View Controller :-

let params = ["name": "admin",
                      "quantity": ""]

        Alamofire.request("Url",method: .post, parameters: params, encoding: JSONEncoding.default, headers: nil).responseObject {
            (response:DataResponse<Login>) in

            switch(response.result) {

            case .success(_):
                if response.result.value != nil{
                    print(response.result.value!)
                }
                break

            case .failure(_):
                print(response.result.error!)
                break
            }
        }

登录:-

 import UIKit
    import ObjectMapper

    class Login : Mappable{

       var CareInsYH:String?
    var CategoryYH:String?

    required init?(map: Map) {

    }

    func mapping(map: Map) {
        CareInsYH <- map["CareInsYH"]
        CategoryYH <- map["CategoryYH"]
    }

   }

Json 响应:-

[
{
        CareInsYH = "";
        CategoryYH = "Type Stock GG - Gordyn        ";
        CollectionCodeYH = 0;
        CollectionsYH = "";
        ColourYH = "";
        CompositionYH = " ";
        ContinuityYH = "";
        CutQuantity = 0;
        DesignYH = "";
        ETAQuantity = 0;
        ExtraId = 14738;
        FRTestYH = "";
        HSCodeYH = "/img/Wash Care/Noimage.jpg";
        IDRPrice = 0;
        LightYH = 0;
        Location1YH = 0;
        Location2YH = 0;
        MartindaleYH = 0;
        MasterId = 14738;
        Name = "SAPPHIRE PTB 8902-1";
        POQuantity = 0;
        PillingYH = " ";
        ProductCode = "GG00787-01C";
        QtyOnHand = "97.72";
        RepeatYH = 0;
        ReservedQuantity = 0;
        RubbingYH = " ";
        SellingPrice = 625000;
        StatusYH = Stock;
        SupColourYH = "";
        SupDesignYH = "";
        SupplierCodeYH = "<null>";
        SupplierNameYH = 0;
        TotalAvlQuantity = "97.72";
        TypeYH = 0;
        USDPrice = 0;
        UsageYH = "";
        WashingYH = "";
        WeightYH = 0;
        WidthYH = "               ";
        WyzenbeekYH = " ";
        imageYH = "/img/products/SGG00787-01C.jpg";
}
]

最佳答案

JSON 响应不正确,正确的示例:

[ 
{
        "CareInsYH": "Pain Management",
        "CutQuantity": 0
    }
]

将您的回复复制到JsonBlob ,这是错误的。

关于swift - 如何将 Response 对象映射到 Alamofire 4.0 Post 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45051939/

相关文章:

ios - 如何访问 UITableView 中具有多个部分的字典数组

ios - 导航栏和 UINavigationController 内容之间的差距

objective-c - 自定义 NSURLProtocol 以显示/隐藏 NetworkActivityIndi​​cator

ios - ReloadData 不适用于 Swift 和 Alamofire

ios - Alamofire 请求为零

swift - 线程 1 : signal SIGABRT Swift Xcode

ios - 如何通过按钮更新分配给自定义单元格中索引路径的核心数据

ios - AFNetworking 一次可以上传多少张图片

arrays - 我如何将这些单独的标签压缩为自定义 UICollectionView 单元格中的标签数组?

ios - 将通用函数从 Alamofire 3 迁移到 Alamofire 4