[英]Assigning a dictionary of type [String:AnyObject] to [String: String] gives nil error
Hey I am newbie in Swift & IOS development. 嘿,我是Swift和IOS开发的新手。 I am facing an issue searched a lot.
我面临一个被大量搜索的问题。 But didn't got any solution.
但是没有任何解决方案。 I have dictionary of type [String: AnyObject] and wants to assign it to an other dictionary of type [String:String].
我有[String:AnyObject]类型的字典,并想将其分配给[String:String]类型的其他字典。 Xcode Shows an error which is this "Fatal error: Unexpectedly found nil while unwrapping an Optional val" I tripled checked there is no nil value.
Xcode显示了一个错误,它是“致命错误:在展开可选val时意外发现nil”,我检查了三倍,发现没有nil值。 Below here is my code snippet Dictionary is here
下面是我的代码片段字典在这里
let dictFormData = ["name": endorsement.name,
"designation":endorsement.designation,
"location": endorsement.location,
"region": endorsement.region,
"effectiveDateString":endorsement.effectiveDate,
"marriageDateString": "ss" ,
"genderId": String(format: "%ld", selectedGenderLookUpId),
"relationId": String(format: "%ld", selectedRelationLookUpId),
"plan": "ss",
"employeeId": AICLPolicyModel.getSelectedPolicy()?.employeeID as Any,
"requestTypeId": 35,
"policyId": AICLPolicyModel.getSelectedPolicy()?.policyID as Any,
"isEmployee": isemployee,
"filePath":"ss",
"fileName": "ss",
"empRemarks": endorsement.remarks,
"hrRemarks": "ss",
"adminRemarks": "ss",
"memberCode": AICLPolicyModel.getSelectedPolicy()?.memberCode as Any,
"requestStatusId": 32,
"customerId": AICLPolicyModel.getSelectedPolicy()?.clientID as Any
]
Here is assigning of one dictionary to other. 这是将一本词典分配给另一本词典。
let formData = (dictFormData as? [String : String])!
At this line it shows error once again I am quite sure there is no nil value. 在这一行,它再次显示错误,我很确定没有nil值。
A dictionary of type [String:AnyObject]
cannot be downcast to [String:String]
since the types are incompatible. 类型
[String:AnyObject]
字典不能向下转换为[String:String]
因为类型不兼容。
Therefore dictFormData as? [String:String]
因此
dictFormData as? [String:String]
dictFormData as? [String:String]
evaluates to nil
because the conditional downcast fails - you are then force unwrapping this nil
with !
dictFormData as? [String:String]
计算结果为nil
因为有条件的向下转换失败-您然后用!
强制解开该nil
!
and you get a crash. 就会崩溃
You are clearly putting non-String values into dictFormData
, so you won't be able to use a simple downcast to get a [String:String]
dictionary. 显然,您正在将非String值放入
dictFormData
,因此您将无法使用简单的向下转换来获得[String:String]
字典。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.