Swift: 从具有 UUID 的数组中删除重复对象
Swift: Removing duplicate objects from array with a UUID
我正在使用 UUID 来使用 DiffableDataSource,但我正在使用一个数据集,该数据集具有每个对象的副本。
这是我在操场上使用的代码示例:
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")
]
由于UUID是在初始化时添加的,在这种情况下是否有合理的方法来删除重复项?
如果您不关心电影的顺序,可以使用 Swift 的集合。
由于您的MovieSearch
符合Hashable协议,Set将确保只存在一个副本。
您可以使用 for-in
表示法遍历集合,类似于数组。
https://developer.apple.com/documentation/swift/set
struct MovieSearch: Hashable, Decodable, Equatable {
let uuid = UUID()
private enum CodingKeys : String, CodingKey { case Name, StartDate }
let Name: String
let StartDate: String
}
var movies:Set = [
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")
]
for movie in movies {
print(movie.Name)
}
所以我猜你想做的是删除那些重复项?
然后编写一个函数,根据电影名称(我猜是唯一的)删除重复项,并用它来过滤。
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
}
然后使用函数:
movies = removeDup(movies: movies)
我正在使用 UUID 来使用 DiffableDataSource,但我正在使用一个数据集,该数据集具有每个对象的副本。
这是我在操场上使用的代码示例:
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")
]
由于UUID是在初始化时添加的,在这种情况下是否有合理的方法来删除重复项?
如果您不关心电影的顺序,可以使用 Swift 的集合。
由于您的MovieSearch
符合Hashable协议,Set将确保只存在一个副本。
您可以使用 for-in
表示法遍历集合,类似于数组。
https://developer.apple.com/documentation/swift/set
struct MovieSearch: Hashable, Decodable, Equatable {
let uuid = UUID()
private enum CodingKeys : String, CodingKey { case Name, StartDate }
let Name: String
let StartDate: String
}
var movies:Set = [
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")
]
for movie in movies {
print(movie.Name)
}
所以我猜你想做的是删除那些重复项?
然后编写一个函数,根据电影名称(我猜是唯一的)删除重复项,并用它来过滤。
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
}
然后使用函数:
movies = removeDup(movies: movies)