Member-only story

3 Major Issues I Faced in February and How I Fixed Them

Jerry PM

--

Bug 1: when check key get nil

The problem is that the dictionary key in selected is “Revenue Growth” (with uppercase) while we’re trying to match it with a lowercase version from keySelected?.indicator .

This a code example that demonstrates the issue with case-sensitive dictionary key matching

// BEFORE - Case-sensitive matching (problematic)
func getFormattedValue(for itemTitle: String, selected: [String: [String: String]]) -> String {
let keySelected = mappedArrCode.first(where: { $0.indicatorCode?.language == itemTitle })
// Direct dictionary access - fails when cases don't match
guard let selectedData = selected[keySelected?.indicator ?? ""],
!selectedData.isEmpty else {
return ""
}
// ... rest of the code
}

// Example data that causes the issue:
let selected = ["Revenue Growth": ["from": "10", "to": "20"]]
let keySelected = "revenue growth" // Different case
// selected[keySelected] will return nil

This the solution:

// AFTER - Case-insensitive matching (solution)
func getFormattedValue(for itemTitle: String, selected: [String: [String: String]]) -> String {
let keySelected = mappedArrCode.first(where: { $0.indicatorCode?.language == itemTitle })
// Case-insensitive key…

--

--

No responses yet