0

嘿,我是 Swift 和 IOS 开发的新手。我面临一个搜索了很多的问题。但没有得到任何解决方案。我有 [String:AnyObject] 类型的字典,并希望将其分配给 [String:String] 类型的其他字典。Xcode 显示一个错误,即“致命错误:在展开可选 val 时意外发现 nil”我三倍检查没有 nil 值。下面是我的代码片段字典在这里

        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
                        ] 

这是将一个字典分配给另一个字典。

   let formData = (dictFormData as? [String : String])!

在这一行它再次显示错误我很确定没有零值。

4

1 回答 1

2

由于类型不兼容,[String:AnyObject]因此无法向下转换类型字典。[String:String]

因此dictFormData as? [String:String]评估为,nil因为有条件的向下转换失败 - 然后你强制解开它nil!你会崩溃。

您显然将非字符串值放入dictFormData中,因此您将无法使用简单的向下转换来获取[String:String]字典。

于 2018-08-11T12:07:47.703 回答