二元运算符 '??'不能应用于 'AnyObject?' 和 'String' 类型的操作数

Binary operator '??' cannot be applied to operands of type 'AnyObject?' and 'String'

我用 Swift 2.2 完成了下面的代码,但是当切换到 Swift 3.0 时,如果条件 "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 语句,如果字典值为 returns nil,则我将空白字符串指定为 ("") 用于特定键

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

例如:

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

这样做:

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

我 运行 在 Swift 3 中遇到与 Date 对象相同的错误。 编译器似乎对此没有问题:

let noStartDate = "No start date" let description = "(\(self.startDate?.toString() ?? noStartDate)) - \(campaignNotes)"