0

In my swift app I've the following function:

func generateTimetable() -> [String:String] {

    var dayCellTFText = [String]()

    for i in 1...7 {
        let cell = tableView.cellForRow(at: [0,i]) as! DayCell
        dayCellTFText.append(cell.timetableTextField.text!)
    }

    let timetable = ["monday":dayCellTFText[0],"thuesday":dayCellTFText[1],"wednesday":dayCellTFText[2],"thursday":dayCellTFText[3],"friday":dayCellTFText[4],"saturday":dayCellTFText[5],"sunday":dayCellTFText[6]]
    return timetable
}

When I invoke the function by, for example, print(generateTimetable()) This is the output:

["thuesday": "", "wednesday": "", "saturday": "", "thursday": "", "monday": "", "friday": "", "sunday": ""]

The problem is that the dictionary's elements changed positions! How can I solve this?

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
Jhon Kafka
  • 13
  • 5

1 Answers1

0

Using a [String : String] is the wrong choice for the return value of the generateTimetable(). If you want an ordered collection you should use arrays. The question is what would be the type contained by the array? Here are two possible solutions:

  1. A tuple :

    func generateTimetable() -> [(String,String)] {
    
        var dayCellTFText = [String]()
    
        for i in 1...7 {
            let cell = tableView.cellForRow(at: [0,i]) as! DayCell
            dayCellTFText.append(cell.timetableTextField.text!)
        }
    
        let timetable = [("monday",dayCellTFText[0]),
                         ("tuesday",dayCellTFText[1]),
                         ("wednesday",dayCellTFText[2]),
                         ("thursday",dayCellTFText[3]),
                         ("friday",dayCellTFText[4]),
                         ("saturday",dayCellTFText[5]),
                         ("sunday",dayCellTFText[6])]
        return timetable
    }
    
  2. A struct

    struct TableEntry {
        let nameOfDay: String
        let textFieldtext: String
    }
    
    func generateTimetable() -> [TableEntry] {
    
        var dayCellTFText = [String]()
    
        for i in 1...7 {
            let cell = tableView.cellForRow(at: [0,i]) as! DayCell
           dayCellTFText.append(cell.timetableTextField.text!)
        }
    
        let timetable = [ TableEntry(nameOfDay: "monday", textFieldtext: dayCellTFText[0]),
                          TableEntry(nameOfDay: "tuesday", textFieldtext: dayCellTFText[0]),
                          TableEntry(nameOfDay: "wednesday", textFieldtext: dayCellTFText[0]),
                          TableEntry(nameOfDay: "thursday", textFieldtext: dayCellTFText[0]),
                          TableEntry(nameOfDay: "friday", textFieldtext: dayCellTFText[0]),
                          TableEntry(nameOfDay: "saturday", textFieldtext: dayCellTFText[0]),
                          TableEntry(nameOfDay: "sunday", textFieldtext: dayCellTFText[0]),
                          TableEntry(nameOfDay: "monday", textFieldtext: dayCellTFText[0])]
        return timetable
    }
    

And thus the return type will be inherently ordered. If you are still interested in sorting a dictionary by its keys, here is an already answered question. But that supposes that the keys already have numerical or alphabetical order (or any sorting order for that matter). But the string "monday", "tuesday", ..., "sunday" are not alphabetically ordered.

ielyamani
  • 17,807
  • 10
  • 55
  • 90