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