AyAz
AyAz

Reputation: 2057

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.

Upvotes: 4

Views: 419

Answers (3)

vadian
vadian

Reputation: 285240

The failure reason is that the dictionary is not initialized at all.

There is a much easier syntax with Swift native types, any type casting is actually not needed because you're just copying AnyObject items. If any value is nil the key is not set.

 for result in result2  {

     var dictData = [String:AnyObject]()

     dictData["offer_title"] = result["offer_title"]
     dictData["discountPercentageOff"] = result["discountPercentageOff"]
...

Or even copying the entire dictionary (in Swift no problem due to value semantics) and removing the unused keys is still more efficient.

Upvotes: 0

Muhammad Noman
Muhammad Noman

Reputation: 1576

 var dictData: NSDictionary?

Change this line to

 var dictData = NSMutableDictionary();

Upvotes: 4

Daniel Klöck
Daniel Klöck

Reputation: 21147

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()

Upvotes: 3

Related Questions