CCDictionary is a class like NSDictionary in Obj-C .
Class CCDictionary
#include <Geode/cocos/cocoa/CCDictionary.h>
ℹ Only the pointer of CCObject or its subclass can be inserted to CCDictionary.
Examples1
// Create a dictionary, return an autorelease object.
CCDictionary* pDict = CCDictionary::create();
// Insert objects to dictionary
CCString* pValue1 = CCString::create("100");
CCString* pValue2 = CCString::create("120");
CCInteger* pValue3 = CCInteger::create(200);
pDict->setObject(pValue1, "key1");
pDict->setObject(pValue2, "key2");
pDict->setObject(pValue3, "key3");
// Get the object for key
CCString* pStr1 = (CCString*)pDict->objectForKey("key1");
CCLog("{ key1: %s }", pStr1->getCString());
CCInteger* pInteger = (CCInteger*)pDict->objectForKey("key3");
CCLog("{ key3: %d }", pInteger->getValue());
Public static methods4
staticcocos2d::CCDictionary*create()
Create a dictionary.
staticcocos2d::CCDictionary*createWithDictionary(cocos2d::CCDictionary*srcDict)
Create a dictionary with an existing dictionary.
srcDict
staticcocos2d::CCDictionary*createWithContentsOfFile(charconst*pFileName)
Create a dictionary with a plist file.
pFileName
staticcocos2d::CCDictionary*createWithContentsOfFileThreadSafe(charconst*pFileName)
Create a dictionary with a plist file.
pFileName
ℹ the return object isn't an autorelease object. This can make sure not using autorelease pool in a new thread. Therefore, you need to manage the lifecycle of the return object. It means that when you don't need it, CC_SAFE_RELEASE needs to be invoked.
Public member functions20
uintcount()
Get the count of elements in CCDictionary.
cocos2d::CCArray*allKeys()
Return all keys of elements.
cocos2d::CCArray*allKeysForObject(cocos2d::CCObject*object)
Get all keys according to the specified object.
⚠️ We use '==' to compare two objects
cocos2d::CCObject*objectForKey()
Get the object according to the specified string key.
key
ℹ The dictionary needs to use string as key. If integer is passed, an assert will appear.
// Assume that the elements are CCString* pointers. Convert it by following code.
CCString* pStr = (CCString*)pDict->objectForKey("key1");
// Do something about pStr.
// If you don't know the object type, properly you need to use dynamic_cast<SomeType*> to check it.
CCString* pStr2 = dynamic_cast<CCString*>(pDict->objectForKey("key1"));
if (pStr2 != NULL) {
// Do something about pStr2
}
cocos2d::CCObject*objectForKey(_::intptr_tkey)
Get the object according to the specified integer key.
key
ℹ The dictionary needs to use integer as key. If string is passed, an assert will appear.
cocos2d::CCStringconst*valueForKey()
Get the value according to the specified string key.
key
ℹ Be careful to use this function since it assumes the objects in the dictionary are CCString pointer.
cocos2d::CCStringconst*valueForKey(_::intptr_tkey)
Get the value according to the specified integer key.
key
ℹ Be careful to use this function since it assumes the objects in the dictionary are CCString pointer.
voidsetObject(cocos2d::CCObject*pObject,)
Insert an object to dictionary, and match it with the specified string key.
pObject
key
ℹ Whe the first time this method is invoked, the key type will be set to string. After that you can't setObject with an integer key. If the dictionary contains the key you passed, the object matching the key will be released and removed from dictionary. Then the new object will be inserted after that.
voidsetObject(cocos2d::CCObject*pObject,_::intptr_tkey)
Insert an object to dictionary, and match it with the specified string key.
pObject
key
ℹ Then the first time this method is invoked, the key type will be set to string. After that you can't setObject with an integer key. If the dictionary contains the key you passed, the object matching the key will be released and removed from dictionary. Then the new object will be inserted after that.
voidremoveObjectForKey()
Remove an object by the specified string key.
key
voidremoveObjectForKey(_::intptr_tkey)
Remove an object by the specified integer key.
key
voidremoveObjectsForKeys(cocos2d::CCArray*pKeyArray)
Remove objects by an array of keys.
pKeyArray
voidremoveObjectForElememt(cocos2d::CCDictElement*pElement)
Remove an object by an element.
pElement
voidremoveAllObjects()
Remove all objects in the dictionary.
virtualcocos2d::CCObject*copyWithZone(cocos2d::CCZone*pZone)
///
cocos2d::CCObject*randomObject()
Return a random object in the dictionary.
boolwriteToFile(charconst*fullPath)
Write a dictionary to a plist file.
fullPath
virtualvoidacceptVisitor(cocos2d::CCDataVisitor&visitor)
charconst*charForKey()
gd::stringgetFirstKey()
Fields2
cocos2d::CCDictElement*m_pElements;
All the elements in dictionary.
ℹ For internal usage, we need to declare this member variable as public since it's used in UT_HASH.
cocos2d::CCDictionary::CCDictTypem_eDictType;
The type of dictionary, it’s assigned to kCCDictUnknown by default.