Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
808 views
in Technique[技术] by (71.8m points)

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

I have a dictionary that populates a Pickerview in Swift 3.

var 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"]

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Now you can do:

"whatever".contains("ER", options: .caseInsensitive)

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"]]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...