比较Swift中不同类型array的元素



我想比较fileNametitle,并将不同的传递给另一个变量。

我想比较fileNametitle,并从allQuiz数组中删除相同的值。

我想要的是这个;让我返回不同的数组类型。

downloadedQuizallQuiz具有相同的数组类型。但是,我想比较两个不同数组类型的变量值

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