比较 Swift 中不同类型数组的元素

Comparing elements of different types of Arrays in Swift

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

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

我要的就是这个;让我return不同的作为数组类型。

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

self.unDownloadedQuiz = self.downloadedQuiz.difference(from: self.allQuiz)

我想比较文件名和标题的值。

我之前使用过这段代码,但这段代码适用于具有相同类型数组的变量。

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]

我的文件模型:

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