swift 在控制器上成功请求时设置POST内部变量

5cnsuln7  于 2023-03-17  发布在  Swift
关注(0)|答案(2)|浏览(92)

我真的很沮丧,所以我只是张贴它,因为我相信有一个简单的答案,但它没有该死的意义。
我收到了从数据库self.fetchFlashDealsWebServiceCall()装载产品的请求
然后,该请求设置我的产品,并且由于来自服务器的分页,一次仅返回10个
令我沮丧的是我需要知道最大页数,它在请求中返回,我在请求中设置它,我知道补全有时会使变量设置不正确,但产品加载,变量设置正确,只是这个该死的属性不会设置。我尝试了很多选项,这个愚蠢的变量已经到了我的极限。我已经验证了它在status中设置正确,但在其他地方也是如此(包括在fetchDataFromServer的末尾)它是nil。我可以将它设置为非可选,但这意味着我必须在viewDidLoad函数内部设置它,而这不会“I don“我也不能工作,因为我必须等待请求方知道。
使用var maxPages: Int?设置ViewController下的变量
获取请求

webService_obj.fetchDataFromServer(alertMsg: false, header:headerTxt, withParameter: PostData as NSDictionary, inVC: self, showHud: false){ (response,message,status) in
            if status{
                let data = response.value(forKey: "data") as! [String: Any]
                let totalPages = data["totalPage"]
                self.maxPages = totalPages as? Int
                
               self.homeBannerData = data["baner"] as! [[String : String]]
                self.imagesbanner =  self.homeBannerData.map{ $0["image"]!}
                
                if self.currentPageNumber == 1 {
                    self.productData = data["product"]  as! [[String:String]]
                } else {
                    self.productData.append(contentsOf: data["product"] as! [[String : String]])
                }
                self.isPageRefresing = false
                
           
            } else {
                self.productData = []
            }
            
            if self.productData.count > 0 {
                self.lbl_no.isHidden = true
            } else {
                self.lbl_no.isHidden = false
            }
            
            print("maxPages == \(self.maxPages) in Fetch Request")
            
            self.collectionview_list.reloadData()
        }

来自服务器的JSON响应

{
        baner =         (
        );
        currentPage = 1;
        product =         (
                        {
                "company_name" = "Sea Dubai";
                created = "51 week ago";
                discount = "";
                "fixcost_product" = y;
                image = "uploads/product/image/342b590dc317e6db9eecef09f5b52b05.png";
                "image_thumb" = "uploads/product/thumb/342b590dc317e6db9eecef09f5b52b05.png";
                logo = "uploads/user/thumb/db95783b008b8a557af4c06a213711da.png";
                name = "WOW Watersports- Go Bot 3 Rider Towable Tubes";
                "offer_end_date" = "12-31-69";
                price = "360.00";
                productViewStatus = "";
                "product_id" = 356;
                "product_variant_id" = 455;
                rating = 5;
                "rating_count" = 0;
                totalSold = "0 Units Sold";
                "user_id" = 135;
            },
                        {
                "company_name" = "Sea Dubai";
                created = "51 week ago";
                discount = 5;
                "fixcost_product" = y;
                image = "uploads/product/image/fb7d00c06eb24d20f4fdd59724fab5b9.png";
                "image_thumb" = "uploads/product/thumb/fb7d00c06eb24d20f4fdd59724fab5b9.png";
                logo = "uploads/user/thumb/db95783b008b8a557af4c06a213711da.png";
                name = "Combined Rope with Chain Forerunner / Stainless Steel";
                "offer_end_date" = "03-28-22";
                price = "151.50";
                productViewStatus = "";
                "product_id" = 355;
                "product_variant_id" = 454;
                rating = 5;
                "rating_count" = 0;
                totalSold = "0 Units Sold";
                "user_id" = 135;
            },
                        {
                "company_name" = "Empire Marine International L.L.C";
                created = "51 week ago";
                discount = 8;
                "fixcost_product" = y;
                image = "uploads/product/image/972ca4126da90706c2b22a4821f3672d.png";
                "image_thumb" = "uploads/product/thumb/972ca4126da90706c2b22a4821f3672d.png";
                logo = "uploads/user/thumb/8a3555b6b16b3d52c7ac33268e10fb8d.jpg";
                name = "SEAGO Horseshoe Lifebuoy with bracket and SOLAS light";
                "offer_end_date" = "03-23-22";
                price = "56.70";
                productViewStatus = "New Releases";
                "product_id" = 354;
                "product_variant_id" = 453;
                rating = 5;
                "rating_count" = 0;
                totalSold = "0 Units Sold";
                "user_id" = 134;
            },
                        {
                "company_name" = "Empire Marine International L.L.C";
                created = "51 week ago";
                discount = "";
                "fixcost_product" = y;
                image = "uploads/product/image/9d394e91f531d04cf2cd8c518cf2144f.png";
                "image_thumb" = "uploads/product/thumb/9d394e91f531d04cf2cd8c518cf2144f.png";
                logo = "uploads/user/thumb/8a3555b6b16b3d52c7ac33268e10fb8d.jpg";
                name = "WINDEX XL Wind Direction Indicator";
                "offer_end_date" = "12-31-69";
                price = "129.00";
                productViewStatus = "";
                "product_id" = 352;
                "product_variant_id" = 450;
                rating = 5;
                "rating_count" = 0;
                totalSold = "0 Units Sold";
                "user_id" = 134;
            },
                        {
                "company_name" = "Empire Marine International L.L.C";
                created = "51 week ago";
                discount = "";
                "fixcost_product" = y;
                image = "uploads/product/image/f4257738fe7f103ab5c61cceb531c075.png";
                "image_thumb" = "uploads/product/thumb/f4257738fe7f103ab5c61cceb531c075.png";
                logo = "uploads/user/thumb/8a3555b6b16b3d52c7ac33268e10fb8d.jpg";
                name = "Transom Platform, stainless steel / teak with foldable ladder";
                "offer_end_date" = "12-31-69";
                price = "345.00";
                productViewStatus = "New Releases";
                "product_id" = 349;
                "product_variant_id" = 447;
                rating = 5;
                "rating_count" = 0;
                totalSold = "2 Units Sold";
                "user_id" = 134;
            },
                        {
                "company_name" = "AER\U00c9 DOCKING SOLUTIONS";
                created = "13 Jan 2022";
                discount = "";
                "fixcost_product" = n;
                image = "uploads/product/image/5cd42aac8aec881886610a2a67ba6081.jpg";
                "image_thumb" = "uploads/product/thumb/5cd42aac8aec881886610a2a67ba6081.jpg";
                logo = "uploads/user/thumb/3dfd2934bc97e0d297c0e89f239703b5.png";
                name = "Fruit request";
                "offer_end_date" = "12-31-69";
                price = "0.00";
                productViewStatus = "New Releases";
                "product_id" = 348;
                "product_variant_id" = 446;
                rating = 5;
                "rating_count" = 0;
                totalSold = "4 Units Sold";
                "user_id" = 88;
            },
                        {
                "company_name" = "USA PARTS AND MORE";
                created = "25 Oct 2021";
                discount = 0;
                "fixcost_product" = n;
                image = "uploads/product/image/711a624e07059edba464ab50f7480103.png";
                "image_thumb" = "uploads/product/thumb/711a624e07059edba464ab50f7480103.png";
                logo = "uploads/user/thumb/89bd178a5695c8ec82e0259ef73f79ca.jpg";
                name = "624610 FRIGIDAIRE door ";
                "offer_end_date" = "12-10-22";
                price = "0.00";
                productViewStatus = "New Releases";
                "product_id" = 347;
                "product_variant_id" = 445;
                rating = 5;
                "rating_count" = 0;
                totalSold = "0 Units Sold";
                "user_id" = 167;
            },
                        {
                "company_name" = "USA PARTS AND MORE";
                created = "25 Oct 2021";
                discount = 0;
                "fixcost_product" = n;
                image = "uploads/product/image/b01409071ed51eaf9601d5c461eabd30.jpg";
                "image_thumb" = "uploads/product/thumb/b01409071ed51eaf9601d5c461eabd30.jpg";
                logo = "uploads/user/thumb/89bd178a5695c8ec82e0259ef73f79ca.jpg";
                name = "624432 WHRILPOOL switch";
                "offer_end_date" = "12-10-22";
                price = "0.00";
                productViewStatus = "New Releases";
                "product_id" = 346;
                "product_variant_id" = 444;
                rating = 5;
                "rating_count" = 0;
                totalSold = "0 Units Sold";
                "user_id" = 167;
            },
                        {
                "company_name" = "USA PARTS AND MORE";
                created = "25 Oct 2021";
                discount = 0;
                "fixcost_product" = n;
                image = "uploads/product/image/20d78421ece6efe54b8247b8788c9a5b.jpg";
                "image_thumb" = "uploads/product/thumb/20d78421ece6efe54b8247b8788c9a5b.jpg";
                logo = "uploads/user/thumb/89bd178a5695c8ec82e0259ef73f79ca.jpg";
                name = "624425 BROAN valve";
                "offer_end_date" = "12-10-22";
                price = "0.00";
                productViewStatus = "New Releases";
                "product_id" = 345;
                "product_variant_id" = 443;
                rating = 5;
                "rating_count" = 0;
                totalSold = "0 Units Sold";
                "user_id" = 167;
            },
                        {
                "company_name" = "USA PARTS AND MORE";
                created = "25 Oct 2021";
                discount = 0;
                "fixcost_product" = y;
                image = "uploads/product/image/7397d3151fd357f9c4a4ddcae49c690f.jpg";
                "image_thumb" = "uploads/product/thumb/7397d3151fd357f9c4a4ddcae49c690f.jpg";
                logo = "uploads/user/thumb/89bd178a5695c8ec82e0259ef73f79ca.jpg";
                name = "624404 SAMSUNG igniter";
                "offer_end_date" = "12-10-22";
                price = "123.00";
                productViewStatus = "New Releases";
                "product_id" = 344;
                "product_variant_id" = 442;
                rating = 5;
                "rating_count" = 0;
                totalSold = "1 Units Sold";
                "user_id" = 167;
            }
        );
        totalPage = 6;
    };
    "error_message" = "";
    status = OK;
    "status_code" = 200;
    "success_message" = success;
}

如果我打印totalPages,它是Optional(6),那么如果我在设置它之后打印self.maxPages,它是nil

tv6aics1

tv6aics11#

这是一种更“健壮”和更安全的方法来解析该值:

let tp = data["totalPage"]
    if let t = tp as? Int {
        print("Setting as Int")
        self.maxPages = t
    } else if let t = tp as? String, let n = Int(t) {
        print("Setting as Int(String)")
        self.maxPages = n
    } else {
        print("Failed to parse data[\"totalPage\"]")
        self.maxPages = 0
    }
ig9co6j1

ig9co6j12#

所以很明显Swift有时候很愚蠢,我不得不将Int值(显然)类型转换为String,如下所示

let totalPages = data["totalPage"]
if let totalPages = totalPages as? String {
    self.maxPages = Int(totalPages)
}

这就给予了我的int值Optional(6),最令人沮丧的一件事,对我来说没有任何意义,我曾经处理过,但希望这能帮助一些人。

相关问题