简体   繁体   English

将[String:AnyObject]类型的字典分配给[String:String]会产生nil错误

[英]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.

相关问题 Swift-使用&#39;as!时为零! 字典 <String, AnyObject> &#39; - Swift - Nil when using 'as! Dictionary<String, AnyObject>' 无法转换类型[[String:AnyObject] ?. Type”(也称为“可选” <Dictionary<String, AnyObject> &gt; .Type)到期望的参数类型 - Cannot convert value of type '[String : AnyObject]?.Type' (aka 'Optional<Dictionary<String, AnyObject>>.Type) to expected argument type Swift-[NSObject:AnyObject]! 不是“字典”的子类型 <String, AnyObject> - Swift - [NSObject : AnyObject]!' is not a subtype of 'Dictionary<String, AnyObject> 扩展可选 <Dictionary<String, AnyObject> &gt; - Extending Optional<Dictionary<String, AnyObject>> “字符串”与“ AnyObject”错误不同 - 'String' is not identical to 'AnyObject' error 尝试保存数组时出现错误-无法将类型[Data]的值转换为预期的参数类型&#39;[Dictionary <String, AnyObject> ]” - Trying to saveArray, got error - Cannot convert value of type '[Data]' to expected argument type '[Dictionary<String, AnyObject>]' “无法下标‘字典’类型的值<nsobject, anyobject> ' 索引类型为 'String' 错误</nsobject,> - "Cannot subscript a value of type 'Dictionary<NSObject, AnyObject>' with an index of type 'String" error 错误无法下标“字典”类型的值 <NSObject, AnyObject> &#39;的索引类型为&#39;String&#39; - Error Cannot subscript a value of type 'Dictionary<NSObject, AnyObject>' with an index of type 'String' (键:AnyObject,值:AnyObject)不能转换为&#39;Dictionary <String, AnyObject> - (key: AnyObject, value: AnyObject) is not convertible to 'Dictionary<String, AnyObject> 如何在 Swift 中将键值对添加到 [String:AnyObject] 类型的字典中? - How to add a key value pair to a dictionary of type [String:AnyObject] in Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM