NSMapTable and NSMutableDictionary differences

前端 未结 4 843
广开言路
广开言路 2020-12-09 19:17

Is an NSMapTable the same as an NSMutableDictionary except for allowing keys to be pointers?

Does it differ in memory management?

4条回答
  •  暖寄归人
    2020-12-09 19:30

    The main difference between NSMapTable and NSMutableDictionary is that NSMapTable stores weak pointers. This means that when you call smth like this:

    [my_table setValue: val forKey: key];
    

    the value and key are not retained (it means no retain message is sent to them). That's why you can use any object (or maybe not object but any pointer) cause they don't have to respond to retain message.

    So you probably want to use NSMapTable if you're using garbage collection where you don't need to bother about retain count of an object.

提交回复
热议问题