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

标签 swift swift3 binary-operators

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

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,则我将空白字符串指定为 ("") 用于特定键

最佳答案

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

例如:

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

关于swift - 二元运算符 '??' 不能应用于类型 'AnyObject?' 和 'String' 的操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39871583/

相关文章:

swift - 在工作表中使用 NavigationLink 后的两个 UINavigationController

ios - 如何禁用第一节@iOS的表格 View 滚动

swift - 如何在 Swift 3.1 中使用字符下标?

C++:将对象添加到当前对象

c - C中(二进制)逻辑运算符语句的行为

c# - DynamicObject 二元运算的绑定(bind)行为因运算符和操作数而异

ios - 在 PageViewController 导航不工作

ios - NSPredicate 核心数据中的多对多关系

swift - 核心数据 - 改变属性类型

ios - 取消分配导航 Controller 中引用自身的 View Controller