Bronze Challenge - still getting the fatal error

I tried adding padding to the columnHeader, but still received a

Swift/ContiguousArrayBuffer.swift:580: Fatal error: Index out of range

Error message…

// for columnLabel in columnLabels {
    for i in 0 ..< dataSource.numberOfColumns {
        let columnLabel = dataSource.label(forColumn: i)
        let columnHeader = " \(columnLabel) |"
        // bronze challenge, trying to pad the column headers
        let paddingNeeded = columnWidths[i] - columnLabel.count
        let padding = repeatElement(" ", count: paddingNeeded).joined(separator: "")
        headerRow += " \(padding)\(columnHeader)"
        columnWidths.append(columnLabel.count)
    }
    print(headerRow)

What did I do wrong?

The too-easy solution would be to add spaces around the “Age” column header, but I’m sure that’s not what the Challenge is looking for.

Also, I changed the separator for the data rows from “” to “stuff”, and I noticed that the separators did not appear around the Age values. Why didn’t they appear?

Table: Department (Engineering)

| Employee Name | Age | Years of Experience |

| stuff stuff stuff stuff stuff stuff stuff stuff stuff Eva | 30 | stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff 6 |

| stuff stuff stuff stuff stuff stuff stuff Saleh | 40 | stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff 28 |

| stuff stuff stuff stuff stuff stuff stuff stuff Amit | 50 | stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff 20 |

I don’t think you understand what they’re asking you to do. For each column, you need to figure out if any of the items being displayed is longer than the header for that column; your code isn’t looking at anything other than the header itself.

As to why your code is crashing: on the first pass through the loop columnWidths is empty. What happens?

As to why the word “stuff” didn’t get printed around the age values, I think that’s because “Age” is only one character longer than the age values being printed. Since it only needed to add one space to the age strings, no separators were needed. If you change the header for that column to something longer (like “Employee Age”, for example) then you would see it.

I took out the " - item.count " clause from paddingNeeded:

// for row in data {
    for i in 0 ..< dataSource.numberOfRows {
        // start the output string
        var out = "|"
        
        // append each item in this row to the string
        // for (j, item) in row.enumerated() {
        for j in 0 ..< dataSource.numberOfColumns {
            let item = dataSource.itemFor(row: i, column: j)
            let paddingNeeded = columnWidths[j] // I took out the - item.count
            let padding = repeatElement(" ", count: paddingNeeded).joined(separator: "")
            out += " \(padding)\(item) |"
        }
        
        // done -- print it!
        print(out)
    }

And I got this output: