这是我在做错误帮助的代码。
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()
}
当我传递nsdictionary作为参数时,它给出了错误可选的nil,你可以看到我创建了每个循环的nsdictionary。但它还没有。我不明白。
答案 0 :(得分:4)
var dictData: NSDictionary?
将此行更改为
var dictData = NSMutableDictionary();
答案 1 :(得分:3)
您永远不会初始化您的词典,var dictData: NSDictionary?
只定义变量的类型。此外,如果你想改变字典,它必须是可变的。
开始于:
var dictData: NSMutableDictionary? = NSMutableDictionary()
答案 2 :(得分:0)
失败的原因是字典根本没有初始化。
使用Swift本机类型有一种更简单的语法,实际上不需要任何类型转换,因为您只是复制AnyObject
项。如果任何值为nil
,则未设置密钥。
for result in result2 {
var dictData = [String:AnyObject]()
dictData["offer_title"] = result["offer_title"]
dictData["discountPercentageOff"] = result["discountPercentageOff"]
...
甚至复制整个字典(在Swift中由于值语义没有问题)并删除未使用的密钥仍然更有效。