Hunter
Hunter

Reputation: 1391

How to remove Index in multiple array if specific String is detected

Ive been searching for at-least an hour with no luck. There are a lot of question similar to this one but that do not give the correct answer.

What I'm trying to do is simply I have an array for a notifications table:

Code:

var notificationList = [(Type: "Work", Date: "7/7/7", Seen: false), (Type: "Home", Date: "8/8/8", Seen: false),(Type: "Fun", Date: "9/9/9", Seen: false)]

It has the value specifying the type of notification it will be, the date it was created and if it has been seen or not.

I can append a new notification to the list by using this.

Code:

 notificationList.append((Type: "Work", Date: "8/8/8", Seen: false))

The problem that I'm trying to find out how to solve is. If I can search for the slot called Date and then say remove all index's that have the string 8/8/8 in the date slot.

Seems simple enough haha.

So far I can do this but this code will make me match every single slot in the array in order for it to work.

Code:

notificationList = notificationList.filter(){$0 != (Type: "Work", Date: "8/8/8", Seen: false)}

Upvotes: 0

Views: 37

Answers (1)

vadian
vadian

Reputation: 285130

To filter for all notifications whose Date is not "8/8/8" write

notificationList = notificationList.filter{ $0.Date != "8/8/8" }

It's highly recommended to use

  • A struct rather than a tuple
  • Lowercased parameter labels / variables since Date can be confused with the struct Date in Swift 3

Upvotes: 1

Related Questions