# collection.columnWidths

{% tabs %}
{% tab title="🌀 Collection" %}
💾 程式： [replit](https://replit.com/@pegasusroe/Swift-Playground#Extenions/Collection+.swift)        ⬆️ 需要： [.allElementsSameLength](https://lochiwei.gitbook.io/ios/swift/collections/collection/collection.allelementssamelength)

````swift
// ┌──────────────────────────────────┐
// │    Collection + .columnWidths    │
// └──────────────────────────────────┘

extension Collection where 
    Element: Collection,            // each element is Collection, e.g. Array.
    Element.Index == Int,           // each element is Int indexed (column index).
    Element.Element: Collection     // each cell is Collection (has `count`), e.g. String.
{
    /// determine the max length (count) in every column of a 2D array of data.
    ///
    /// ### Explanation:
    /// ```
    ///   typealias Data  = [[String]]   // Data is Array, which is Collection.
    ///   typealias Row   = [String]     // Row == Data.Element, Array (Int indexed).
    ///   typealias Value = String       // Value == Row.Element, String, which is Collection too
    /// ```
    /// ### Example:
    /// - `[["a", "bc", "def"], ["ab", "cde", "f"]].columnWidths == Optional([2, 3, 3])`
    /// - `[["a", "b", "c"], ["a"]].columnWidths == nil`
    public var columnWidths: [Int]? {
        
        guard 
            allElementsSameLength,       // all rows same length
            let firstRow = first,        // data must at least have one row
            !firstRow.isEmpty            // row must at least have one cell
        else {
            return nil
        }
        
        // for each column (at index j), find that column's max string length.
        // every column contains at least one string, so max length (max()) can't be nil.
        return (0 ..< firstRow.count).map { j in 
            self                                // collection of elements (rows)
                .map { row in row[j].count }    // array of j-th elements' lengths
                .max()!                         // ⭐ force unwrap (max string length)
        }
    }
}
````

{% endtab %}

{% tab title="👥 相關" %}

* uses [collection.allelementssamelength](https://lochiwei.gitbook.io/ios/swift/collections/collection/collection.allelementssamelength "mention").
* used by [logger](https://lochiwei.gitbook.io/ios/swift/debugging/logger "mention") (in .<mark style="color:purple;">**table**</mark>() method).
  {% endtab %}
  {% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/ios/swift/collections/collection/collection.columnwidths.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
