二元运算符'??'不能应用于类型为“AnyObject?"的操作数和“字符串" [英] Binary operator '??' cannot be applied to operands of type 'AnyObject?' and 'String'

查看:30
本文介绍了二元运算符'??'不能应用于类型为“AnyObject?"的操作数和“字符串"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 Swift 2.2 完成了以下代码,但是当切换到 Swift 3.0 时,在 if 条件二元运算符 '??' 出现错误不能应用于类型为AnyObject?"的操作数和'字符串'"

I have done below code with Swift 2.2, but when switched to Swift 3.0 getting error at if condition "Binary operator '??' cannot be applied to operands of type 'AnyObject?' and 'String'"

if let custID = dataDict["cust_id"] ?? "",
let custName = dataDict["cust_name"] ?? "",
let fileName = dataDict["filename"] ?? "",
let transNumber = dataDict["trans_no"] ?? "" {

linesheet_custID = (custID["text"] ?? "" ) as! String
linesheet_custName = (custName["text"] ?? "" ) as! String
linesheet_filename = (fileName["text"] ?? "" ) as! String
linesheet_TransNumber = (transNumber["text"] ?? "" ) as! String
}

请提出解决方案,如上面的 if let 语句中的代码,如果字典值返回 nil 那么我为特定键分配了空白字符串作为 ("")

Please suggest solution ,as in above code in if let statement if dictionary value returns nil then i assigned blank string as ("") for particular key

推荐答案

您应该将从字典中获得的值转换为可选字符串.

You should cast the values you get from the dictionaries to optional Strings.

例如:

let custID = (dataDict["cust_id"] as String?) ?? ""

这篇关于二元运算符'??'不能应用于类型为“AnyObject?"的操作数和“字符串"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆