print(itemToRefresh)
print(cell.item!.id)
if (itemToRefresh == cell.item!.id) {
print("MATCHED")
}
输出:
Optional("PI096")
Optional("PI096")
"匹配"不打印。为什么呢?
答案 0 :(得分:0)
itemToRefresh是一个字符串,但事实证明,在我的代码中,我已经像这样分配了它:
让itemToRefresh =" \(foo)"
应该是
让itemToRefresh =" \(foo!)"
答案 1 :(得分:0)
应该让itemToRefresh =“(foo!)”为例如
let foo: String? = "ABC"
let itemToRefresh = "\(foo!)" // will be -> ABC
// let itemToRefresh = "\(foo)" // will be -> optional(ABC)
// and optional(ABC) will not be equal to ABC
let itemb = "ABC"
print(itemToRefresh) // ABC
print(itemb) // ABC
if itemToRefresh == itemb { // true
print("123") // print 123
}
如果您愿意,可以安全地解包可选值并检查
if (foo != nil) {
let itemToRefresh = "\(foo!)" // will be -> ABC
// let itemToRefresh = "\(foo)" // will be -> "optional(ABC)"
let itemb = "ABC"
print(itemToRefresh)
print(itemb)
if itemToRefresh == itemb {
print("123")
}
}