我想比较fileName
和title
,并将不同的传递给另一个变量。
我想比较fileName
和title
,并从allQuiz
数组中删除相同的值。
我想要的是这个;让我返回不同的数组类型。
downloadedQuiz
和allQuiz
具有相同的数组类型。但是,我想比较两个不同数组类型的变量值
self.unDownloadedQuiz = self.downloadedQuiz.difference(from: self.allQuiz)
我想比较fileName和title的值。
我以前使用过这段代码,但这段代码适用于具有相同类型数组的变量。
self.unDownloadedQuiz = self.downloadedQuiz.difference(from: self.allQuiz)
extension Array where Element: Hashable {
func difference(from other: [Element]) -> [Element] {
let thisSet = Set(self)
let otherSet = Set(other)
return Array(thisSet.symmetricDifference(otherSet))
}
}
价值观:
var myFile: [MyFile]
ver allQuiz: [Quiz]
MyFile模型:
struct MyFile {
var fileName: String
....
}
测试模型:
struct Quiz: Codable, Realmable, Hashable {
var title: String = ""
....
}
使用您提供的数组扩展名,看起来您想要这样做,这是文件的fileName
属性数组与测验的title
属性数组的比较:
let files: [MyFile] = //...
let quizzes: [Quiz] = //...
let result : [String] = files.map(.fileName).difference(from: quizzes.map(.title))