Cocos2d-x specific changes about CCDictionary and CCArray

Cocos2d-x specific changes

Following are changes Cocos2d-X specific.

CCDictionary and CCArray

1. Why CCMutableDictionary is Deprecated?

In gles20 branch, 'CCMutableDictionary' and 'CCMutableArray' are deprecated. You should use CCDictionary and CCArray instead.
CCDictionary is implemented by UTHash. Compared with the old CCMutableDictionary which is implemented by stl, the efficiency of the CCDictionary 
is increased by at least two times. Also, CCDictionary now does not use template of cpp, so it can easily be bound to script.

2. Key Type?

Currently, CCDictionary supports two kinds of key type, which are 'std::stirng' and 'int'. One CCDictionary instance only supports one unique key type.
It is confirmed once you first invoke 'setObject'.

3. How to Use CCDictionary?

The way of using CCDictionary is almost the same as CCMutableDictionary. We kept the same api and 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 :
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
      CCMutableDictionary<std::string, CCObjectSubClass*>               |                     CCDictionary   
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
   std::vector<std::string> keys = theDict->allKeys();                  |         CCDictElement* pElement = NULL;
   std::vector<std::string>::iterator it;                               |         CCDICT_FOREACH(theDict, pElement)
   for (it = keys.begin(); it != keys.end(); it++)                      |         {
   {                                                                    |            CCObjectSubClass* pSubClassObj = (CCObjectSubClass*)pElement->getObject();
        std::string oneKey = *it;                                       |            // You can also get the current key, but make sure you know the key type.
        CCObjectSubClass* pSubClassObj = theDict->objectForKey(oneKey); |            std::string oneStrKey = pElement->getStrKey(); // if the key type is string.
        // do some operation by using pSubClass pointer.                |            // int oneIntKey = pElement->getIntKey(); // if the key type is integer.
        ...                                                             |            // do some operation by using pSubClass pointer.
        ...                                                             |            ...
   }                                                                    |         }   
                                                                        |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

We kept CCDictionary::allKeys method, so you can also use the same way as CCMutableDictionary 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 and then using CCARRAY_FOREACH to traverse dictionary.

If you want to iterate CCDictionary at lua codes, of course, you can't use the CCDICT_FOREACH macro, in this case, you should use the old way to make it works.

4. Why CCMutableArray is deprecated?

In cocos2d-x 1.0.1, we have CCArray and CCMutableArray, which make users very confused,
and CCMutableArray also uses template and stl, the disadvantage is the same as CCMutableDictionary.
The old CCArray does not support reversely iterate array, we have provided the macro -- CCARRAY_FOREACH_REVERSE to achieve that.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值