Ios 筛选Coredata类型的数组
我的tableview单元格中有一个字符串。现在我想检查该字符串是否在特定数组中。如果该数组有字符串,我想从该数组中删除该字符串。这就是我所尝试的 同样在下面,favMessages是一个名为Ios 筛选Coredata类型的数组,ios,swift,core-data,Ios,Swift,Core Data,我的tableview单元格中有一个字符串。现在我想检查该字符串是否在特定数组中。如果该数组有字符串,我想从该数组中删除该字符串。这就是我所尝试的 同样在下面,favMessages是一个名为FavoritedMessages var textInCell = cell.historyTextLabel.text for k in favMessages { if let thefavData = k.value(forKey: "favData") as? String {
FavoritedMessages
var textInCell = cell.historyTextLabel.text
for k in favMessages {
if let thefavData = k.value(forKey: "favData") as? String {
if sdf == thefavData {
favMessages = favMessages.filter{$0 != textInCell} // HERE ERROR IS THROWN 'Binary operator '!=' cannot be applied to operands of type 'FavoritedMessages' and 'String?''
}
}
}
我如何解决我无法解决的问题…你可以这样做
favMessages = favMessages.filter({$0["Your key name of database"] as! String != "string to be compared"})
就你而言
favMessages = favMessages.filter({$0["favData"] as! String != textInCell})
希望这对您有帮助您有一个带字符串的数组:
var strings = ["a", "b", "c", "d"]
用于删除的元素:
var firstElement = "a"
var secondElement = "e"
print("Before removing: ", strings)
//Before removing: ["a", "b", "c", "d"]
[firstElement, secondElement].forEach {
if let index = strings.index(of: $0) {
strings.remove(at: index)
}
}
结果:
print("After removing: ", strings)
After removing: ["b", "c", "d"]
在下面的代码中编写属性名而不是StringValue
favMessages = favMessages.filter{$0.StringValue != textInCell}
在下面的代码中编写属性名而不是StringValue。favMessages=favMessages.filter{$0.StringValue!=textInCell}favMessages数组中存储了哪些对象?你的建议有效吗。你能把它作为一个答案,让我接受吗。。?