Dictionary

  • 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.

    // [Int64: [Player]]
    let cursor = try Player.fetchCursor(db)
    let dictionary = try Dictionary(grouping: cursor, by: { $0.teamID })
    
  • 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.

    // [Int64: [Player]]
    let cursor = try Player.fetchCursor(db)
    let dictionary = try Dictionary(
        minimumCapacity: 100,
        grouping: cursor,
        by: { $0.teamID })
    
  • Creates a new dictionary from the key-value pairs in the given cursor.

    You use this initializer to create a dictionary when you have a cursor of key-value tuples with unique keys. Passing a cursor with duplicate keys to this initializer results in a fatal error.

    // [Int64: Player]
    let cursor = try Player.fetchCursor(db).map { ($0.id, $0) }
    let playerByID = try Dictionary(uniqueKeysWithValues: cursor)
    

    Precondition

    The cursor must not have duplicate keys.
  • Creates a new dictionary from the key-value pairs in the given cursor.

    You use this initializer to create a dictionary when you have a cursor of key-value tuples with unique keys. Passing a cursor with duplicate keys to this initializer results in a fatal error.

    // [Int64: Player]
    let cursor = try Player.fetchCursor(db).map { ($0.id, $0) }
    let playerByID = try Dictionary(
        minimumCapacity: 100,
        uniqueKeysWithValues: cursor)
    

    Precondition

    The cursor must not have duplicate keys.