Silver Challenge Solution


Does anyone have the solution to the silver challenge they can share? I’m hung up on it.


If I remember correctly, the problem is in this last line:

// append each item in this row to our string for j in 0 ..< dataSource.numberOfColumns { let item = dataSource.itemForRow(i, column: j) let itemString = " \(item) |" let paddingAmount = columnWidths[j] - itemString.characters.count
because if itemString is longer than the column header, then the padding(amount:) function fails trying to execute an invalid loop. So columnWidths[j] needs to be the maximum width of any item in the column, not just of the header item.

[code] for (column, columnLabel) in columnLabels.enumerate() {
let columnHeader = " (columnLabel) |"

    // determine item with maximum width in column
    for row in 0 ..< dataSource.numberOfRows {
        let itemString = " \(dataSource.itemForRow(row, column: column)) |"
        columnWidths[column] = max(columnWidths[column], itemString.characters.count)
    firstRow += padding(columnWidths[column] - columnHeader.characters.count) + "\(columnHeader)"