Post marked as solved
Post marked as solved with 2 replies, 284 views
A couple of unit tests for an application failed on a iOS 17.2.1 device and I could break down the problem to some strange behaviour when applying a dictionary + DateComponents keys. The tests had been running fine with iOS 16.x
Here is some simplified code to reproduce the behaviour in Playground:
let date1 = try! Date("2023-12-31T00:00:00Z", strategy: .iso8601)
let date2 = try! Date("2024-01-31T00:00:00Z", strategy: .iso8601)
let date3 = try! Date("2024-02-28T00:00:00Z", strategy: .iso8601)
let date1dc = Calendar.current.dateComponents([.year, .month], from: date1)
let date2dc = Calendar.current.dateComponents([.year, .month], from: date2)
let date3dc = Calendar.current.dateComponents([.year, .month], from: date3)
let dc1 = DateComponents(year: 2023, month: 12)
let dc2 = DateComponents(year: 2024, month: 01)
let dc3 = DateComponents(year: 2024, month: 02)
let data: [DateComponents: String] = [
dc1: "One", dc2: "Two", dc3: "Three"
]
print(date1dc == dc1)
print(date2dc == dc2)
print(date3dc == dc3)
print("--------------------------------")
print(data[dc1])
print(data[dc2])
print(data[dc3])
print("--------------------------------")
print(data[date1dc])
print(data[date2dc])
print(data[date3dc])
The output for date1dc, date2dc and date3dc now is random:
true
true
true
--------------------------------
Optional("One")
Optional("Two")
Optional("Three")
--------------------------------
Optional("One")
nil
Optional("Three")
or
true
true
true
--------------------------------
Optional("One")
Optional("Two")
Optional("Three")
--------------------------------
nil
nil
nil
or
true
true
true
--------------------------------
Optional("One")
Optional("Two")
Optional("Three")
--------------------------------
nil
nil
Optional("Three")
For me it looks like a serious foundation bug, but maybe I'm missing something.