Outdated Docs > CCDictionary

CCDictionary

Introduction

CCDictionary is implemented using UTHash. In contrary, the old deprecated CCMutableDictionary was implemented using stl. The efficiency of the CCDictionary is now increased by at least TWO Folds. Also, CCDictionary now does not use template of cpp, so it can easily be bound to script.

Key Type

CCDictionary supports two kinds of key type, which are ‘std::string’ and ‘int’. One CCDictionary instance only supports one key type, it is locked when you first invoke ‘setObject’.

Traversal

We removed the ‘begin’, ‘end’ and ‘next’ method which are used in iterating the whole dictionary. Instead, we use CCDICT_FOREACH macro to achieve that. The way of using CCDICT_FOREACH is quite the same as CCARRAY_FOREACH.

The following will illustrate how to iterate CCDictionary :

CCDictElement* pElement = NULL;

CCDICT_FOREACH(theDict, pElement)

{

    CCObjectSubClass* pSubClassObj = (CCObjectSubClass*)pElement->getObject();

    // You can also get the current key, but make sure you know the key type.

    std::string oneStrKey = pElement->getStrKey(); // if the key type is string.

    // int oneIntKey = pElement->getIntKey(); // if the key type is integer.

    // do some operation by using pSubClass pointer.

}   

The CCDictionary::allKeys method is kept, so you can also use to iterate dictionary. But we strongly don’t advise you to do that. Becasue the performance of CCDICT_FOREACH is much higher than getting all key. If you want to iterate CCDictionary at lua codes you can’t use the CCDICT_FOREACH macro. in this case, you should use the old way to make it works.

Sign up for our newsletter to keep up with the latest developments, releases and updates for Cocos2d-x.