14

I came across NSFrozenDictionary while debugging an app.

NSFrozenDictionary

Shared index property declared as NSDictionary * sharedIndex = ...

What is it? How is it different from NSMutableDictionary?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
0rt
  • 1,325
  • 2
  • 17
  • 25
  • 5
    It's internal classes/wrapper. Use it as a casual `NSDictionary`. Strangely, I would have considered it as mutable (since there is a "M" at the end", but debugger treats it as a NSDictionary (not mutable), and the "frozen" tends too to say so. – Larme Jan 16 '18 at 22:03
  • I assume "M" at the end is short for implementation, maybe to explicitly differentiate it from a protocol type? – villapossu Nov 16 '18 at 21:26

2 Answers2

12

It is an NSMutableDictionary marked as immutable.

One case to get __NSFrozenDictionaryM:

  1. Have an array of mutable dictionaries:

    NSArray *array = @[{NSMutableDictionary}, {NSMutableDictionary}, {NSMutableDictionary}]

  2. Making a two-level-deep copy of it by:

    NSArray *res = [[NSArray alloc] initWithArray:array copyItems:YES]

The resulting res array contains immutable copies of NSMutableDictionaries in array, which are of type __NSFrozenDictionaryM. I guess this is an optimisation to avoid really copying all dictionaries in the original array.

Zhigang An
  • 296
  • 2
  • 13
3

It is one of the concrete subclasses that is part of the NSDictionary class cluster.

There is a more academic description on Apple's documentation site.

Essentially: don't worry about it. If you declared it as a plain NSDictionary, then treat it as such: an immutable dictionary. Foundation may create something else under the hood for optimization purposes, but as far as your code is concerned, it's still an immutable dictionary.

Steve Madsen
  • 13,465
  • 4
  • 49
  • 67