This must be simple for whoever has experience I guess. All I am trying to do is to put together in an array of dictionaries the days of the month with their respective days of the week using nested for
-loops.
For example:
I create an instance of the array like so:
let arrayOfDictionaries:[[String:Any]] = [[String:Any]]()
I later create an array of the days of the week:
let daysOfWeek = ["Monday","Tuesday"..."Sunday"]
Then I have a for loop for the 31 days of the month of October, with for
loop inside regarding the days of the week:
for i in 1...31 {
for day in daysOfWeek{
arrayOfDictionaries.append(["Monday",1])
....
}
}
At the end I want to end up with arrayOfDictionaries
looking like so (Assuming October would start on a Monday):
[["Monday",1],["Tuesday",2]...["Monday",8],["Tuesday",9]...["Monday",16],["Tuesday",17]...and so on up to the 31st]
Any help would be much appreciated.
This can be solved using the modulo operator!
var arrayOfDictionaries:[[String:Any]] = [[String:Any]]()
let daysOfWeek = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
for i in 1...31 {
arrayOfDictionaries.append([daysOfWeek[((i - 1) % 7)]:i])
}