Initializer

init(_unsafeUninitializedCapacity:allowingDuplicates:initializingWith:)

Creates a new dictionary with the specified capacity, then calls the given closure to initialize its contents.

Declaration

init(_unsafeUninitializedCapacity capacity: Int, allowingDuplicates: Bool, initializingWith initializer: (UnsafeMutableBufferPointer<Key>, UnsafeMutableBufferPointer<Value>) -> Int)

Parameters

capacity

The capacity of the new dictionary.

allowingDuplicates

If false, then the caller guarantees that all keys are unique. This promise isn’t verified – if it turns out to be false, then the resulting dictionary won’t be valid.

body

A closure that can initialize the dictionary’s elements. This closure must return the count of the initialized elements, starting at the beginning of the buffer.

Discussion

Foundation uses this initializer to bridge the contents of an NSDictionary instance without allocating a pair of intermediary buffers. Pass the required capacity and a closure that can intialize the dictionary’s elements. The closure must return c, the number of initialized elements in both buffers, such that the elements in the range 0..<c are initialized and the elements in the range c..<capacity are uninitialized.

The resulting dictionary has a count less than or equal to c. The actual count is less iff some of the initialized keys were duplicates. (This cannot happen if allowingDuplicates is false.)

The buffers passed to the closure are only valid for the duration of the call. After the closure returns, this initializer moves all initialized elements into their correct buckets.