Reputation: 153
How can I get a date from an array if I don't have all the parts of the date.
I have a dictionary with different dates and try to get all dates from it with the month of June.
let dict = ["15-06-2019" : "red",
"19-10-2019" : "green",
"05-08-2018" : "yellow",
"20-06-2019" : "orange"]
Upvotes: 0
Views: 65
Reputation: 285079
Use filter
let dict = ["15-06-2019" : "red", "19-10-2019" : "green", "05-08-2018" : "yellow", "20-06-2019" : "ornge"]
let filtered = dict.filter { $0.key.range(of: "-06-") != nil }
Upvotes: 3
Reputation: 164
I'm not sure I understand what you're trying to do here, but based on the structure of the dictionary I'm presuming the dates work day-month-year. If that's the case and you're trying to just get the dates in June the code below should work.
let dict = ["15-06-2019" : "red", "19-10-2019" : "green", "05-08-2018" : "yellow", "20-06-2019" : "ornge"]
var juneDates = [String]()
for (keys, values) in dict{
let dates = keys.components(separatedBy: "-")
if dates[1] == "06"{
juneDates.append(keys)
print(juneDates)
}
}
Upvotes: 0