2020年1月10日金曜日

Swift Dictionary Grouping


     There is the comment in the Dictionary souce code.

    /// Creates a new dictionary whose keys are the groupings returned by the

    /// given closure and whose values are arrays of the elements that returned
    /// each key.
    ///
    /// The arrays in the "values" position of the new dictionary each contain at
    /// least one element, with the elements in the same order as the source
    /// sequence.
    ///
    /// The following example declares an array of names, and then creates a
    /// dictionary from that array by grouping the names by first letter:


let students = ["Kofi", "Abena", "Efua", "Kweku", "Akosua"]
let studentsByLetter = Dictionary(grouping: students, by: { $0.first! })

0 件のコメント:

コメントを投稿