Why is NSDictionary a null?

This is the code where I am making a mistake.

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 gives an optional nil error, and you can see that I created nsdictionary every loop. but it's still zero. I can not understand.

+5
source share
3 answers
  var dictData: NSDictionary? 

Change this line to

  var dictData = NSMutableDictionary(); 
+4
source

Do you never initialize a dictionary, var dictData: NSDictionary? defines only the type of type. In addition, if you want to change the dictionary, it must be changed.

Start with:

 var dictData: NSMutableDictionary? = NSMutableDictionary() 
+3
source

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

There is a much simpler syntax with native Swift types; casting of any type is not really needed because you are just copying AnyObject elements. 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 (there is no problem in Swift due to the semantics of the value) and deleting unused keys is even more efficient.

0
source

All Articles