cocos2d-x  2.2.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
CCDictionary Class Reference

@ More...

#include <CCDictionary.h>

Inheritance diagram for CCDictionary:
CCObject CCCopying

Public Member Functions

 CCDictionary ()
 The constructor of CCDictionary. More...
 
 ~CCDictionary ()
 The destructor of CCDictionary. More...
 
unsigned int count ()
 Get the count of elements in CCDictionary. More...
 
CCArrayallKeys ()
 Return all keys of elements. More...
 
CCArrayallKeysForObject (CCObject *object)
 Get all keys according to the specified object. More...
 
CCObjectobjectForKey (const std::string &key)
 Get the object according to the specified string key. More...
 
CCObjectobjectForKey (intptr_t key)
 Get the object according to the specified integer key. More...
 
const CCStringvalueForKey (const std::string &key)
 Get the value according to the specified string key. More...
 
const CCStringvalueForKey (intptr_t key)
 Get the value according to the specified integer key. More...
 
void setObject (CCObject *pObject, const std::string &key)
 Insert an object to dictionary, and match it with the specified string key. More...
 
void setObject (CCObject *pObject, intptr_t key)
 Insert an object to dictionary, and match it with the specified string key. More...
 
void removeObjectForKey (const std::string &key)
 Remove an object by the specified string key. More...
 
void removeObjectForKey (intptr_t key)
 Remove an object by the specified integer key. More...
 
void removeObjectsForKeys (CCArray *pKeyArray)
 Remove objects by an array of keys. More...
 
void removeObjectForElememt (CCDictElement *pElement)
 Remove an object by an element. More...
 
void removeAllObjects ()
 Remove all objects in the dictionary. More...
 
CCObjectrandomObject ()
 Return a random object in the dictionary. More...
 
bool writeToFile (const char *fullPath)
 Write a dictionary to a plist file. More...
 
virtual void acceptVisitor (CCDataVisitor &visitor)
Function override
virtual CCObjectcopyWithZone (CCZone *pZone)
 This function is used for deepcopy elements from source dictionary to destination dictionary. More...
 
- Public Member Functions inherited from CCObject
 CCObject (void)
virtual ~CCObject (void)
void release (void)
void retain (void)
CCObjectautorelease (void)
CCObjectcopy (void)
bool isSingleReference (void) const
unsigned int retainCount (void) const
virtual bool isEqual (const CCObject *pObject)
virtual void update (float dt)

Static Public Member Functions

static CCDictionarycreate ()
 Create a dictionary. More...
 
static CCDictionarycreateWithDictionary (CCDictionary *srcDict)
 Create a dictionary with an existing dictionary. More...
 
static CCDictionarycreateWithContentsOfFile (const char *pFileName)
 Create a dictionary with a plist file. More...
 
static CCDictionarycreateWithContentsOfFileThreadSafe (const char *pFileName)
 Create a dictionary with a plist file. More...
 

Public Attributes

CCDictElementm_pElements
 All the elements in dictionary. More...
 
- Public Attributes inherited from CCObject
unsigned int m_uID
int m_nLuaID

Additional Inherited Members

- Protected Attributes inherited from CCObject
unsigned int m_uReference
unsigned int m_uAutoReleaseCount

Detailed Description

@

// Create a dictionary, return an autorelease object.
// 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());

Constructor & Destructor Documentation

The constructor of CCDictionary.

The destructor of CCDictionary.

Member Function Documentation

virtual void acceptVisitor ( CCDataVisitor visitor)
virtual

Reimplemented from CCObject.

CCArray* allKeys ( )

Return all keys of elements.

Returns
The array contains all keys of elements. It's an autorelease object yet.
CCArray* allKeysForObject ( CCObject object)

Get all keys according to the specified object.

Warning
We use '==' to compare two objects
Returns
The array contains all keys for the specified object. It's an autorelease object yet.
virtual CCObject* copyWithZone ( CCZone pZone)
virtual

This function is used for deepcopy elements from source dictionary to destination dictionary.

You shouldn't invoke this function manually since it's called by CCObject::copy.

Reimplemented from CCCopying.

unsigned int count ( )

Get the count of elements in CCDictionary.

Returns
The count of elements.
static CCDictionary* create ( )
static

Create a dictionary.

Returns
A dictionary which is an autorelease object.
See Also
createWithDictionary(CCDictionary*), createWithContentsOfFile(const char*), createWithContentsOfFileThreadSafe(const char*).
static CCDictionary*
createWithContentsOfFile
( const char *  pFileName)
static

Create a dictionary with a plist file.

Parameters
pFileNameThe name of the plist file.
Returns
A dictionary which is an autorelease object.
See Also
create(), createWithDictionary(CCDictionary*), createWithContentsOfFileThreadSafe(const char*).
static CCDictionary*
createWithContentsOfFileThreadSafe
( const char *  pFileName)
static

Create a dictionary with a plist file.

Note
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.
Parameters
pFileNameThe name of the plist file.
Returns
A dictionary which isn't an autorelease object.
static CCDictionary*
createWithDictionary
( CCDictionary srcDict)
static

Create a dictionary with an existing dictionary.

Parameters
srcDictThe exist dictionary.
Returns
A dictionary which is an autorelease object.
See Also
create(), createWithContentsOfFile(const char*), createWithContentsOfFileThreadSafe(const char*).
CCObject* objectForKey ( const std::string &  key)

Get the object according to the specified string key.

Note
The dictionary needs to use string as key. If integer is passed, an assert will appear.
Parameters
keyThe string key for searching.
Returns
The object matches the key. You need to force convert it to the type you know.
// 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
}
See Also
objectForKey(intptr_t)
CCObject* objectForKey ( intptr_t  key)

Get the object according to the specified integer key.

Note
The dictionary needs to use integer as key. If string is passed, an assert will appear.
Parameters
keyThe integer key for searching.
Returns
The object matches the key.
See Also
objectForKey(const std::string&)
CCObject* randomObject ( )

Return a random object in the dictionary.

Returns
The random object.
See Also
objectForKey(intptr_t), objectForKey(const std::string&)
void removeObjectForElememt ( CCDictElement pElement)

Remove an object by an element.

Parameters
pElementThe element need to be removed.
See Also
removeObjectForKey(const std::string&), removeObjectForKey(intptr_t), removeObjectsForKeys(CCArray*), removeAllObjects().
void removeObjectForKey ( const std::string &  key)

Remove an object by the specified string key.

Parameters
keyThe string key for searching.
See Also
removeObjectForKey(intptr_t), removeObjectsForKeys(CCArray*), removeObjectForElememt(CCDictElement*), removeAllObjects().
void removeObjectForKey ( intptr_t  key)

Remove an object by the specified integer key.

Parameters
keyThe integer key for searching.
See Also
removeObjectForKey(const std::string&), removeObjectsForKeys(CCArray*), removeObjectForElememt(CCDictElement*), removeAllObjects().
void removeObjectsForKeys ( CCArray pKeyArray)

Remove objects by an array of keys.

Parameters
pKeyArrayThe array contains keys to be removed.
See Also
removeObjectForKey(const std::string&), removeObjectForKey(intptr_t), removeObjectForElememt(CCDictElement*), removeAllObjects().
void setObject ( CCObject pObject,
const std::string &  key 
)

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

Note
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.
Parameters
pObjectThe Object to be inserted.
keyThe string key for searching.
See Also
setObject(CCObject*, intptr_t)
void setObject ( CCObject pObject,
intptr_t  key 
)

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

Note
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.
Parameters
pObjectThe Object to be inserted.
keyThe string key for searching.
See Also
setObject(CCObject*, const std::string&)
const CCString* valueForKey ( const std::string &  key)

Get the value according to the specified string key.

Note
Be careful to use this function since it assumes the objects in the dictionary are CCString pointer.
Parameters
keyThe string key for searching
Returns
An instance of CCString. It will return an empty string if the objects aren't CCString pointer or the key wasn't found.
See Also
valueForKey(intptr_t)
const CCString* valueForKey ( intptr_t  key)

Get the value according to the specified integer key.

Note
Be careful to use this function since it assumes the objects in the dictionary are CCString pointer.
Parameters
keyThe string key for searching.
Returns
An instance of CCString. It will return an empty string if the objects aren't CCString pointer or the key wasn't found.
See Also
valueForKey(intptr_t)
bool writeToFile ( const char *  fullPath)

Write a dictionary to a plist file.

Parameters
fullPathThe full path of the plist file. You can get writeable path by getWritablePath()
Returns
true if successed, false if failed

Member Data Documentation

CCDictElement* m_pElements

All the elements in dictionary.

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

The documentation for this class was generated from the following file: