使用不区分大小写的搜索过滤字典 [英] Filter Dictionary with a case insensitive search
问题描述
这是关于如何过滤词典的后续问题我需要过滤器不区分大小写
This is a follow-up question on How to Filter a Dictionary only I need the filter to be case-insensitive
我有一个字典,用于填充Swift 3中的Pickerview。
I have a dictionary that populates a Pickerview in Swift 3.
var facilityDict: [Int: [String: String]] = [:]
17:[id:199,facilitycode:036,location_name:Centerpoint
医疗办公室 ],
41:[id:223,facilitycode:162,
location_name:暗岭医疗中心],
14:[id:196,
facilitycode:023,location_name:Spinnerpark],
20:[id:
202,facilitycode:048,location_name:教育剧场,
30:[id:212,facilitycode:090, location_name:合作伙伴
医疗办公室],
49:[id:231,facilitycode:223,
location_name: GreenBay行政办公室]
17: ["id": "199", "facilitycode": "036", "location_name": "Centerpoint Medical Offices"],
41: ["id": "223", "facilitycode": "162", "location_name": "Dark Ridge Medical Center"],
14: ["id": "196", "facilitycode": "023", "location_name": "Spinnerpark"],
20: ["id": "202", "facilitycode": "048", "location_name": "Educational Theater"],
30: ["id": "212", "facilitycode": "090", "location_name": "Partner Medical Offices"],
49: ["id": "231", "facilitycode": "223", "location_name": "GreenBay Administrative Offices"]
字典很长。我有一个 Textbox
在数据更改时触发,我用它来创建搜索变量。如果有人输入ar,我想过滤字典中包含字符ar的所有内容,以便我可以使用原始大字典列表的小得多的子部分重置Picker列表。
The Dictionary is quite long. I have a Textbox
that fires when data is changed and I use this to create a search variable. If someone types in "ar", I want to filter every thing in the dictionary that has the characters "ar" in it so that I can reset the Picker list with a much smaller subsection of the original Big Dictionary list.
过滤器
let filteredDictionaries = facilityDict.filter {$ 0.value.contains(其中:{$ 0.value.contains( ar。)})}。map {$ 0.value}
似乎很好地过滤了字典,但是我需要搜索不区分大小写。我可以设置选项以将过滤器显示更改为不区分大小写吗?
The filter
let filteredDictionaries = facilityDict.filter{$0.value.contains(where: {$0.value.contains("ar")})}.map{$0.value}
seems to filter the dictionary well, However I need the search to be case-insensitive. Are there options I can set to change the filter show to case insensitive?
推荐答案
包含方法与<$ c相同$ c> range(of:String)!= nil 没有任何选项。您只需要使用String!= nil和caseInsensitive选项的范围:
Contains method it is the same as range(of: "String") != nil
without any options. All you need is to use range of String != nil with caseInsensitive options:
extension String {
func contains(_ string: String, options: CompareOptions) -> Bool {
return range(of: string, options: options) != nil
}
}
现在你可以这样做:
"whatever".contains("ER", options: .caseInsensitive)
如果你需要从你的字典数组中创建一个字典,你需要使用forEach迭代结果并从中重建字典:
If you need to create a dictionary from your array of dictionaries, you would need to use forEach to iterate through the result and rebuild your dictionary from it:
let facilityDict: [Int: [String: String]] = [
17: ["id": "199", "facilitycode": "036", "location_name": "Centerpoint Medical Offices"],
41: ["id": "223", "facilitycode": "162", "location_name": "Dark Ridge Medical Center"],
14: ["id": "196", "facilitycode": "023", "location_name": "Spinnerpark"],
20: ["id": "202", "facilitycode": "048", "location_name": "Educational Theater"],
30: ["id": "212", "facilitycode": "090", "location_name": "Partner Medical Offices"],
49: ["id": "231", "facilitycode": "223", "location_name": "GreenBay Administrative Offices"]]
var filtered: [Int: [String: String]] = [:]
facilityDict.filter{$0.value.contains{$0.value.contains("AR", options: .caseInsensitive)}}.forEach{filtered[$0.key] = $0.value}
print(filtered) // [30: ["id": "212", "facilitycode": "090", "location_name": "Partner Medical Offices"], 41: ["id": "223", "facilitycode": "162", "location_name": "Dark Ridge Medical Center"], 14: ["id": "196", "facilitycode": "023", "location_name": "Spinnerpark"]]
这篇关于使用不区分大小写的搜索过滤字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!