skip to Main Content

I’m getting really frustrated so I’m just posting it because I’m sure there’s an easy answer but it makes no dang sense.

I have this request to load products from a database
self.fetchFlashDealsWebServiceCall()

That request then sets my products, and only returns 10 at a time due to pagination from the server

What’s frustrating me is I need to know the max pages, and it’s returned in the request, and I set it in the request, and I understand that completions can make it so variables aren’t set properly sometimes but the products load, the variables are set properly, and it’s just this one gosh dang property that won’t set. I have tried a lot of options, and I’m just at the end of my rope with this stupid variable. I’ve verified it IS set properly inside of status but everywhere else (including at the end of the fetchDataFromServer) it is nil. I could set it to not be optional but that means I have to set it inside of the viewDidLoad function and that won’t work either because I have to wait for the request to know.

Set the variable under the ViewController with var maxPages: Int?

Fetch Request

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 Response from Server

{
        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" = "AERU00c9 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;
}

If I print totalPages it’s Optional(6)
then if I print self.maxPages after setting it it’s nil

2

Answers


  1. Chosen as BEST ANSWER

    So apparently Swift is just idiotic sometimes and I had to typecast the (clearly) Int value to a String with the following

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

    and that worked to give the Optional(6) value to my int. Single most frustrating thing that doesn't make any sense to me that I've ever dealt with but hopefully this helps someone.


  2. This is a bit more "robust" and safer way to parse that value:

        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
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search