user8977455
user8977455

Reputation:

Filtering an array of Coredata type

I have a string in my tableview cell. Now I want to check if that string is in a specific array. And if that array has the string, I want to delete the string from that array. This is what I have tried for that...

Also below, favMessages is an array of coredata object called 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?''
        }
    }
}

How I can resolve it that I'm not able to figure out...

Upvotes: 1

Views: 64

Answers (3)

Sagar Chauhan
Sagar Chauhan

Reputation: 5823

Write your attribute name instead of StringValue in following code.

favMessages = favMessages.filter{$0.StringValue != textInCell}

Upvotes: 1

biloshkurskyi.ss
biloshkurskyi.ss

Reputation: 1436

You have an array with strings:

var strings = ["a", "b", "c", "d"]

Elements for removing:

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)
    }
}

Result:

print("After removing: ", strings)
After removing:  ["b", "c", "d"]

Upvotes: 1

Hitesh Sultaniya
Hitesh Sultaniya

Reputation: 939

You can do like this,

favMessages = favMessages.filter({$0["Your key name of database"] as! String != "string to be compared"})

In your case,

favMessages = favMessages.filter({$0["favData"] as! String != textInCell})

Hope this helps

Upvotes: 0

Related Questions