Class CCDictionary

#include <Geode/cocos/cocoa/CCDictionary.h>
classCCDictionary:publiccocos2d::CCObject{ ... }

CCDictionary is a class like NSDictionary in Obj-C .

ℹ 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.

Return value
A dictionary which is an autorelease object.
staticcocos2d::CCDictionary*createWithDictionary()

Create a dictionary with an existing dictionary.

Parameters

srcDict

The exist dictionary.
Return value
A dictionary which is an autorelease object.
staticcocos2d::CCDictionary*createWithContentsOfFile(
charconst*pFileName
)

Create a dictionary with a plist file.

Parameters

pFileName

The name of the plist file.
Return value
A dictionary which is an autorelease object.
staticcocos2d::CCDictionary*createWithContentsOfFileThreadSafe(
charconst*pFileName
)

Create a dictionary with a plist file.

Parameters

pFileName

The name of the plist file.
Return value
A dictionary which isn't an autorelease object.

ℹ 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.

Return value
The count of elements.
cocos2d::CCArray*allKeys()

Return all keys of elements.

Return value
The array contains all keys of elements. It's an autorelease object yet.
cocos2d::CCArray*allKeysForObject()

Get all keys according to the specified object.

Return value
The array contains all keys for the specified object. It's an autorelease object yet.

⚠️ We use '==' to compare two objects

cocos2d::CCObject*objectForKey()

Get the object according to the specified string key.

Parameters

key

The string key for searching.
Return value
The object matches the key. You need to force convert it to the type you know.

ℹ 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()

Get the object according to the specified integer key.

Parameters

key

The integer key for searching.
Return value
The object matches the 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.

Parameters

key

The string key for searching
Return value
An instance of CCString. It will return an empty string if the objects aren't CCString pointer or the key wasn't found.

ℹ Be careful to use this function since it assumes the objects in the dictionary are CCString pointer.

cocos2d::CCStringconst*valueForKey()

Get the value according to the specified integer key.

Parameters

key

The string key for searching.
Return value
An instance of CCString. It will return an empty string if the objects aren't CCString pointer or the key wasn't found.

ℹ Be careful to use this function since it assumes the objects in the dictionary are CCString pointer.

voidsetObject(,)

Insert an object to dictionary, and match it with the specified string key.

Parameters

pObject

The Object to be inserted.

key

The string key for searching.

ℹ 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(,)

Insert an object to dictionary, and match it with the specified string key.

Parameters

pObject

The Object to be inserted.

key

The string key for searching.

ℹ 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.

Parameters

key

The string key for searching.
voidremoveObjectForKey()

Remove an object by the specified integer key.

Parameters

key

The integer key for searching.
voidremoveObjectsForKeys()

Remove objects by an array of keys.

Parameters

pKeyArray

The array contains keys to be removed.
voidremoveObjectForElememt()

Remove an object by an element.

Parameters

pElement

The element need to be removed.
voidremoveAllObjects()

Remove all objects in the dictionary.

virtualcocos2d::CCObject*copyWithZone()

///

cocos2d::CCObject*randomObject()

Return a random object in the dictionary.

Return value
The random object.
boolwriteToFile(
charconst*fullPath
)

Write a dictionary to a plist file.

Parameters

fullPath

The full path of the plist file. You can get writeable path by getWritablePath()
Return value
true if successed, false if failed
virtualvoidacceptVisitor()
No description provided
charconst*charForKey()
No description provided
gd::stringgetFirstKey()
No description provided
Fields2
;

All the elements in dictionary.

ℹ For internal usage, we need to declare this member variable as public since it's used in UT_HASH.

;

The type of dictionary, it’s assigned to kCCDictUnknown by default.

Protected member functions0
Protected fields0