Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why NSDictionary is nil?

This is code where i am doing mistake help.

         for result in (result2 ){

            var dictData: NSDictionary?

             dictData?.setValue(result.valueForKey("offer_title") as? String, forKey:"offer_title")
              print(result.valueForKey("offer_title") as? String)
            if result.valueForKey("discountPercentageOff") as? String != "" {
                dictData?.setValue(result.valueForKey("discountPercentageOff") as? String, forKey:"discountPercentageOff")
                print(result.valueForKey("discountPercentageOff") as? String)
            }

            dictData?.setValue(result.valueForKey("is_booked") as? Bool, forKey:"is_booked")
             print(result.valueForKey("is_booked"))
            dictData?.setValue(result.valueForKey("is_saved") as? Bool, forKey:"is_saved")
            print(result.valueForKey("is_saved"))

            dictData?.setValue(result.valueForKey("area_id") as? String, forKey:"area_id")
            print(result.valueForKey("area_id"))

            dictData?.setValue(result.valueForKey("area_name") as? String, forKey:"area_name")

            dictData?.setValue(result.valueForKey("business_id") as? String, forKey:"business_id")
            dictData?.setValue(result.valueForKey("business_name") as? String, forKey:"business_name")
            dictData?.setValue(result.valueForKey("category_id") as? String, forKey:"category_id")
            dictData?.setValue(result.valueForKey("category_name") as? String, forKey:"category_name")
            dictData?.setValue(result.valueForKey("expiry_date") as? String, forKey:"expiry_date")
            dictData?.setValue(result.valueForKey("featured") as? String, forKey:"featured")
            dictData?.setValue(result.valueForKey("locality") as? String, forKey:"locality")
            dictData?.setValue(result.valueForKey("offer_available") as? String, forKey:"offer_available")
            dictData?.setValue(result.valueForKey("offer_id") as? String, forKey:"offer_id")
            dictData?.setValue(result.valueForKey("offer_image") as? String, forKey:"offer_image")
            dictData?.setValue(result.valueForKey("offer_nature") as? String, forKey:"offer_nature")
            dictData?.setValue(result.valueForKey("offerNatureValue") as? String, forKey:"offerNatureValue")
            dictData?.setValue(result.valueForKey("on_going") as? Bool, forKey:"on_going")
            dictData?.setValue(result.valueForKey("discountOriginalPrice") as? String, forKey:"discountOriginalPrice")
            dictData?.setValue(result.valueForKey("discountDiscountedPrice") as? String, forKey:"discountDiscountedPrice")

            let offer = Offer()
            offer.setDatafromServer(dictData!)
             self.saveOfferData(item as! NSDictionary)
            print("item:\(dictData)")

            self._featuredOfferArray.addObject(offer)
            self.feturedOfferTableView.reloadData()



        }

when i pass nsdictionary as param it give error optional nil, and you can see i created nsdictionary each loop. but it is yet nil. i can not understand.

like image 624
AyAz Avatar asked Mar 12 '23 02:03

AyAz


2 Answers

 var dictData: NSDictionary?

Change this line to

 var dictData = NSMutableDictionary();
like image 158
Muhammad Noman Avatar answered Mar 19 '23 18:03

Muhammad Noman


You never initialize your dictionary, var dictData: NSDictionary? only defines what kind of type the variable will be. Also, if you want to change the dictionary, it has to be mutable.

Start with:

var dictData: NSMutableDictionary? = NSMutableDictionary()
like image 26
Daniel Avatar answered Mar 19 '23 16:03

Daniel