skip to Main Content

I’m trying to loop through a set of Users, and remove them from another set LostFollowers (if they exist). Users are identifiable by id.

struct LostFollower {
  let user: User
  let dateLost: Date
}

let users1: Set<LostFollower> = // …
let users2: Set<User> = // …

users2.forEach { user in
  // Need to remove anyone in the set users1, whose `user` property is equal to user
}

How can I do this? Will I need to use filter or is there a better way to do it?

Note: Set operations won’t work because users1 and users2 are of different types.

2

Answers


  1. if i understand the question correctly, you’re trying to remove elements from users1 ( set of LostFollowers ) if they exists in users2.
    I think you can do something like this

    let filteredUser = users1.filter { follower in 
        !users2.contains(follower.user)
    }
    
    Login or Signup to reply.
  2. let filtered = users2.subtracting(users1.map { $0.user })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search