Usually the switch-case statement works like this: if the condition exactly matches the value, then the corresponding code block is executed. But in my case, something went wrong! Why does the code for "update" work for me while the condition is "date" !? I tried to recreate the situation in the playground - everything works correctly there. What is the problem?
Why does the update block work when key = "date"? Theoretically, the default block should work!?
My code:
print("Dictionary = \(dictionary)") for (key, value) in dictionary { switch key { case BaseDatabase.COLUMN_ID: // My code case WeddingDatabase.COLUMN_PREMIUM: // My code case BaseDatabase.COLUMN_UPDATE_CLEAN: print("type = \(BaseDatabase.COLUMN_UPDATE_CLEAN), key = \(key)") // My code default: // My default code } } My console:
dictionary = ["note": <null>, "date": 2024-08-08 00:00:00, "update": 2019-07-09 08:57:05, "id_wedding": 1] type = update, key = date // WHY?? type = update, key = update UPDATE:
Cut the code to the banal:
let key = "date" switch key { case "update": print("key = \(key)") break default: print("default = \(key)") break } Added code in viewDidLoad empty viewController. The console still displays key = date. I see the problem ONLY in my project. I tried to add code in a new project and in playgroud - everything works fine (default = date is output to the console). How can this be? I tried different Simulators (and iOS versions) - the problem is still there. I also tried to clean the project - the problem persists. Maybe somewhere in the project the work of switch statement is redefined - is this possible?
BaseDatabase.COLUMN_ID,WeddingDatabase.COLUMN_PREMIUMandBaseDatabase.COLUMN_UPDATE_CLEAN?static let COLUMN_ID = "_id",static let COLUMN_PREMIUM = "premium"andprivate static let COLUMN_UPDATE_CLEAN = "update"static func ~= (lhs: String, rhs: String) -> Bool { return lhs.contains(rhs) }manifests the problem, too. Second, you should not put answers in your question, but rather post your own answer to your question. I think this a real gem you’ve found, but you’re making it hard for someone to vote for your question, much less your answer.