init(dictionary:copyItems:)
Initializes a newly allocated dictionary using the objects contained in another given dictionary.
Declaration
convenience init(dictionary otherDictionary: [AnyHashable : Any], copyItems flag: Bool)Parameters
- otherDictionary:
A dictionary containing the keys and values with which to initialize the new dictionary.
- flag:
If True, each object in
otherDictionaryreceives a Copywithzone: message to create a copy of the object—objects must conform to theNSCopyingprotocol. In a managed memory environment, this is instead of theretainmessage the object would otherwise receive. The object copy is then added to the returned dictionary.If False, then in a managed memory environment each object in
otherDictionarysimply receives aretainmessage when it is added to the returned dictionary.
Return Value
An initialized object—which might be different than the original receiver—containing the keys and values found in otherDictionary.
Discussion
After an immutable dictionary has been initialized in this way, it cannot be modified.
The copy(with:) method performs a shallow copy. If you have a collection of arbitrary depth, passing true for the flag parameter will perform an immutable copy of the first level below the surface. If you pass false the mutability of the first level is unaffected. In either case, the mutability of all deeper levels is unaffected.