Ios 快速搜索最近的相似值,不考虑序列

Ios 快速搜索最近的相似值,不考虑序列,ios,arrays,swift,Ios,Arrays,Swift,嗨,伙计们,我想问一下,你们是如何在这些项目中搜索最接近的相似值的。例如,当我搜索[Restaurant,Bull]时,它应该返回我str2是可能最接近的值。因为此函数只能用于顺序,所以不能用于非顺序。我真的希望你们能帮我 func search(`for` searchItems: Set<String>, `in` searchArea: [Set<String>]) -> Set<String>? { return sea

嗨,伙计们,我想问一下,你们是如何在这些项目中搜索最接近的相似值的。例如,当我搜索[Restaurant,Bull]时,它应该返回我str2是可能最接近的值。因为此函数只能用于顺序,所以不能用于非顺序。我真的希望你们能帮我

 func search(`for` searchItems: Set<String>, `in` searchArea: [Set<String>]) -> Set<String>? {
            return searchArea.max(by: { (a, b) -> Bool in
                return searchItems.intersection(a).count < searchItems.intersection(b).count || searchItems.intersection(a).count > searchItems.intersection(b).count
            })
        }        


            let str2: Set<String> = ["Bull","Restaurant","Corner"]
            let str3: Set<String> = ["Corner","Restaurant","Mole"]

            let area = [str3, str2] as [Any]

            print("search result",self.search(for: ["Restaurant","Bull"], in: area as! [Set<String>]))

只需对查询数组进行设置,并使用其isSubsetof:方法检查它是否是数据的子集

let set1 = Set([1,2,3])
let set2 = Set([2,3,4])

let query = [1,2]
let querySet = Set(query)

querySet.isSubset(of: set1) // true
querySet.isSubset(of: set2) // false

let query2 = [2,1]
let querySet2 = Set(query)

querySet2.isSubset(of: set1) // true
querySet2.isSubset(of: set2) // false
可能是因为您的str2和str3根本没有设置,它是数组,因为您使用的是数组声明,因此将其更改为此,然后如果您使用[Bull,Restaurant],它就会工作:


另外,Set是非有序序列,Array是有序序列

对于非序列,你说不能是什么意思?这意味着当我使用[restaurant,bull]搜索时,如果使用[bull,restaurant]它不会。。。这就是问题所在
let str2 = Set(["Bull","Restaurant","Corner"])
let str3 = Set(["Corner","Restaurant","Mole"])