Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Garut

macrumors newbie
Original poster
Sep 7, 2021
6
0
I have an app I am trying to build that contains player objects in an array but I am trying to make sure there are no duplicates. My objects come by way of a notification from another VC. How do I remove duplicates in my favArr?




Swift:
class FavouriteManager {


   


   


    static let shared = FavouriteManager()


   


    var favArr : [CurrentPlayer] = []


   





   


    func add(_ player: CurrentPlayer) {


        NotificationCenter.default.post(


            name: .passFavNotification,


            object: player


        )


       





        favArr.append(player)


       

        //algo I tried but did not work.
        for player in favArr {


            if !favArr.contains(player) {


                favArr.append(player)


            }


    }
}
}










Swift:
extension Notification.Name {


    static let passFavNotification = Notification.Name("pass")


}


class HockeyDetailVC: UITableViewController {



[B]     var[/B] item: CurrentPlayer?


     override func viewDidLoad() {
   
       NotificationCenter.default.addObserver(self,


        selector: #selector(handleFavNotification(notification:)),


        name: .passFavNotification,


        object: nil)


    }


   


    @objc func handleFavNotification(notification:Notification){


            if let theFav = notification.object as? CurrentPlayer {


                item = theFav


            }


        }
}
 
Alright I figured it out just needed to add this extension.


Swift:
extension Array where Element: Hashable {


    func removingDuplicates() -> [Element] {


        var addedDict = [Element: Bool]()


        


        return filter {


            addedDict.updateValue(true, forKey: $0) == nil


        }


    }


    


    mutating func removeDuplicates() {


        self = self.removingDuplicates()


    }


}
 
Alright I figured it out just needed to add this extension.


Swift:
extension Array where Element: Hashable {


    func removingDuplicates() -> [Element] {


        var addedDict = [Element: Bool]()


       


        return filter {


            addedDict.updateValue(true, forKey: $0) == nil


        }


    }


   


    mutating func removeDuplicates() {


        self = self.removingDuplicates()


    }


}

Sure thing - go for what works. But this feels a bit like a hack, if all you want to achieve is not having duplicates, you can just use a Set instead of an array.
Or if your elements conform to Equatable, you can use something similar to your first post; Before ever appending anything you can check if the array already contains the element. - But you may be trying to add and then remove duplicates for some reason rather than avoiding having them in the first place? Or am I missing something else?
 
Sure thing - go for what works. But this feels a bit like a hack, if all you want to achieve is not having duplicates, you can just use a Set instead of an array.
Or if your elements conform to Equatable, you can use something similar to your first post; Before ever appending anything you can check if the array already contains the element. - But you may be trying to add and then remove duplicates for some reason rather than avoiding having them in the first place? Or am I missing something else?

Agreed. Definitely an inefficient approach.
 
  • Like
Reactions: ventmore
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.