I am using a UUID to get the use of DiffableDataSource but I am working with a data set that has a duplicate of every object.
Here is a sample of the code I am working with from a playground:
var movies: [MovieSearch] = []
struct MovieSearch: Hashable, Decodable, Equatable {
let uuid = UUID()
private enum CodingKeys : String, CodingKey { case Name, StartDate }
let Name: String
let StartDate: String
}
movies = [
MovieSearch(Name: "Blade Runner", StartDate: "01/01/2021"),
MovieSearch(Name: "Blade Runner: 2049", StartDate: "01/07/2021"),
MovieSearch(Name: "UBIK", StartDate: "01/14/2021"),
MovieSearch(Name: "Blade Runner", StartDate: "01/01/2021"),
MovieSearch(Name: "Blade Runner: 2049", StartDate: "01/07/2021"),
MovieSearch(Name: "UBIK", StartDate: "01/14/2021")
]
Since the UUID is added at the time of initialization is there a reasonable way to delete the duplicates in this scenario?
So I'm guessing what you want to do is to remove those duplicates?
Then write a function that removes the duplicates based on the name of the movie (which I'm guessing is unique) and use it to filter.
func removeDup(movies: [MovieSearch]) -> [MovieSearch] {
var uniqueMovies = Set<String>()
var moviesWithoutDups = [MovieSearch]()
for movie in movies {
if !uniqueMovies.contains(movie.name) {
moviesWithoutDups.append(movie)
uniqueMovies.insert(movie.name)
}
}
return moviesWithoutDups
}
Then use the function:
movies = removeDup(movies: movies)