在控制器成功请求后设置内部变量

问题描述 投票:0回答:0

我真的很沮丧,所以我只是发布它,因为我确定有一个简单的答案,但这没有任何意义。

我有这个请求从数据库加载产品

self.fetchFlashDealsWebServiceCall()

然后该请求设置我的产品,由于服务器分页,一次只返回 10 个

让我沮丧的是我需要知道最大页数,它在请求中返回,我在请求中设置它,我知道完成可以做到这一点,所以有时变量设置不正确但产品加载,变量设置正确,只是这个 gosh dang 属性不会设置。我已经尝试了很多选择,但我对这个愚蠢的变量感到无能为力。我已经验证它在

status
内部正确设置,但在其他任何地方(包括在 fetchDataFromServer 的末尾)它都是零。我可以将它设置为不是可选的,但这意味着我必须将它设置在 viewDidLoad 函数中,这也不起作用,因为我必须等待请求才能知道。

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;
}
ios swift post global
© www.soinside.com 2019 - 2024. All rights reserved.