Home

dictionaryWithDictionary

dictionaryWithDictionary is a class method of NSDictionary that creates and returns a new dictionary containing the entries of a given dictionary. The resulting dictionary is immutable and holds the same key-value pairs as the input. The method performs a shallow copy: the keys and values themselves are not copied, only the dictionary structure, and the objects referenced by those keys and values are reused.

The method signature is + (NSDictionary *)dictionaryWithDictionary:(NSDictionary *)dictionary;. It serves as a convenience constructor, equivalent to alloc/initWithDictionary:,

If the input dictionary is mutable, the new dictionary remains immutable. Passing nil to the method will

Common usage includes producing an immutable copy from a mutable dictionary or cloning an existing dictionary

Alternative initializers include -initWithDictionary:, and other copy operations such as -copy or -mutableCopy, depending on whether

---

but
typically
returns
an
autoreleased
object
under
manual
reference
counting.
With
ARC,
memory
management
is
handled
automatically.
usually
raise
an
exception
in
Objective-C.
in
a
concise
form.
Example:
NSDictionary
*src
=
@{@"a":
@1,
@"b":
@2};
NSDictionary
*copy
=
[NSDictionary
dictionaryWithDictionary:src];
a
mutable
or
immutable
result
is
desired.
dictionaryWithDictionary:
is
primarily
used
for
straightforward,
explicit
creation
of
an
immutable
dictionary
from
an
existing
dictionary
without
manually
allocating
and
initializing
a
new
instance.