i just want an empty NSMutableDictionary

Discussion in 'Mac Programming' started by Howiieque, Aug 19, 2009.

  1. Howiieque macrumors regular

    Feb 1, 2009
    i just want to have an empty NSMutableDictionary.

    [[NSMutableDictionary alloc] init]


    [[NSMutableDictionary] alloc] initWithCapacity:0]

    it seems that both of them work.
    could some one explain which one i should use.
    thank you in advanced.:)
  2. Sijmen macrumors 6502a

    Sep 7, 2005
    The plain -init version probably calls initWithCapacity with the default value. What that value may be, I'm not sure.

    There's one more way by the way:

    [NSMutableDictionary new];

    which is a shorthand method for:

    [[NSMutableDictionary alloc] init];
  3. cqexbesd macrumors regular

    Jun 4, 2009
    I would speculate that the former creates a dictionary with the default capacity while the later will create one with a capacity of 0. Assuming you later want to put something in the dictionary you would be better off with the former.

    I don't know anything about the implementation of NSMutableDictionary but I would guess that the capacity parameter controls how many buckets you get so setting this to too low a number will cause excessive collisions and destroy performance. In a trivial implementation of Dictionary then setting capacity to 0 would prevent you from putting anything in the dictionary at all however it's possible that dictionaries will dynamically adjust bucket count or the constructor has a minimum number of buckets to create. I haven't tried it.


  4. kpua macrumors 6502

    Jul 25, 2006

    The capacity parameter is just a hint to NSMutableDictionary. It is by no means limited to the initial size, and will dynamically expand.

    The purpose for this is to avoid repeated grows when it is known beforehand that X number of items are going to be inserted.


    This all means that if you aren't concerned about memory allocation / reallocation being an issue, then it doesn't really matter. AFAIK, -init is equivalent to -initWithCapacity:0.
  5. Howiieque thread starter macrumors regular

    Feb 1, 2009
    thank you guys.
    according to The Designated Initializer, init might be overridden. i was just not sure. thank you for your answers.:)
  6. Cinder6 macrumors 6502


    Jul 9, 2009
    You can also use [NSMutableDictionary dictionary], if you want to use a convenience method.
  7. Howiieque thread starter macrumors regular

    Feb 1, 2009
    thank you for your suggestion.:)
    because some authors said that reduce using the auto release pool would benefit the performance, so i use the alloc and init.
  8. Catfish_Man macrumors 68030


    Sep 13, 2001
    Portland, OR
    Autorelease pools are fast, don't worry about it. If you find your app going slowly, you should use Shark and Instruments to find out why, and *then* make it fast.
  9. Cinder6 macrumors 6502


    Jul 9, 2009
    You only really need to worry about the autorelease pool if you're low on memory and in the middle of a large loop.

Share This Page