异构集合文字只能推断为'[String:Any]';如果这是故意的,请添加显式类型注释

时间:2017-07-23 18:36:31

标签: ios swift3 alamofire xcode8

我有一个像这样的POST主体参数:

Line number: 1 - u'CD-SKU'
0H041501, VOL3380235300029, 3380235300029, B00FIZJI0K, 11.59, None, None, None, None, None, None, None, None, None
Traceback (most recent call last):
File "/home/mws/Env/mws_prod/local/lib/python2.7/site-packages/import_export/resources.py", line 434, in import_row
instance, new = self.get_or_init_instance(instance_loader, row)
File "/home/mws/Env/mws_prod/local/lib/python2.7/site-packages/import_export/resources.py", line 258, in get_or_init_instance
instance = self.get_instance(instance_loader, row)
File "/home/mws/Env/mws_prod/local/lib/python2.7/site-packages/import_export/resources.py", line 252, in get_instance
return instance_loader.get_instance(row)
File "/home/mws/Env/mws_prod/local/lib/python2.7/site-packages/import_export/instance_loaders.py", line 31, in get_instance
field = self.resource.fields[key]
KeyError: u'CD-SKU'

因为我正在使用Alamofire发布参数,我正在描述这样的帖子体字典:

{
  "id": 0,
  "name": "string",
  "contactInfo": "string",
  "message": "string"
}

但我得到的错误是这样的: Error screenshot

我在这种语法中做错了什么?

2 个答案:

答案 0 :(得分:18)

如果无法推断类型,则必须对其进行注释:

let body : [String:Any] = ["id": userID, "name": userName, "contactInfo": contactInfo, "message": message]

答案 1 :(得分:2)

您应该为变量添加[String: Any]显式类型。

let body = ["id": userID, 
            "name": userName,
            "contactInfo": contactInfo,
            "message": message] as [String: Any]