Swift Array intersection by property -
i trying compare 2 arrays. 1 array array of person objects, each of has email property string email address. other array emailaddress object has descriptive word "work" or "personal" , actual string email address.
basically both objects have string property email address. want compare these arrays of objects see if 1 of objects each array has same email address. right using nested for loops shown below taking long.
for person in self.allpeople! { e in emailaddresses! { if e.value == person.email { return true } } } i thought using set intersection looked work comparing same objects , not object's properties. thanks.
you can still use set functionality first creating set of emails. map helps turn 1 collection another, in case changing collection of allpeople collection of people's emails. faster because emailaddresses iterated once, instead of once per person.
let personemails = set(self.allpeople!.map { $0.email }) let matchingemails = emailaddresses!.map { $0.value } return !personemails.isdisjoint(with: matchingemails)
Comments
Post a Comment