Case Insensitive Dictionary in Swift

How to initialize a case insensitive dictionary using Swift?

You can use reduce(into:) method and assign each element capitalized to the result:

let array = ["Sam", "sam", "SAM"]
let dict: [String: [String]] = array.reduce(into: [:]) {
$0[$1.capitalized, default: []].append($1)
}

print(dict) // ["Sam": ["Sam", "sam", "SAM"]]

Filter Dictionary with a case insensitive search

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

how to sort array of dictionary by case insensitive in swift

How about:

let sortedArray = episode_ParticipantList.sort{
$0["FirstName"]?.lowercaseString < $1["FirstName"]?.lowercaseString
}

This might be faster, but you'll need to test:

import Foundation

let sortedArray = episode_ParticipantList.sort{
$0["FirstName"]!.caseInsensitiveCompare($1["FirstName"]!) == .OrderedAscending
}

Case insensitive keys for NSDictionary

Lately I've shared two classes that might suit your needs. They provide case-insensitive operations (named after NSDictionary and NSMutableDictionary methods) while keeping the originally inserted keys.

Give it a try:

https://github.com/keeshux/ios-components/tree/master/Components/Utils/KSCIDictionary

allHeaderFields is not case insensitive when looking up keys

The bug is not related with Alamofire. This is a Swift bug.

Unfortunately this is a known bug that appeared at Swift 3 when they transformed the headers into Dictionary. The bug is registered since 2016 and remains unsolved. To make things worst they not even corrected the Swift documentation.

This is a violation of the HTTP specification and I have no idea why they flagged this only as a Medium priority. Seems that they do not care and will never solve this problem.

A workaround was made by Stephen Gurnett

Bug Report



Related Topics



Leave a reply



Submit