cocos2d-x  3.3rc0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
__Dictionary Class Reference

Dictionary is a class like NSDictionary in Obj-C . More...

#include <CCDictionary.h>

Inheritance diagram for __Dictionary:
Ref Clonable

Public Member Functions

 __Dictionary ()
 The constructor of Dictionary. More...
 
 ~__Dictionary ()
 The destructor of Dictionary. More...
 
bool init ()
 Initializes the dictionary. More...
 
unsigned int count ()
 Get the count of elements in Dictionary. More...
 
local count ()
 Get the count of elements in Dictionary. More...
 
__ArrayallKeys ()
 Return all keys of elements. More...
 
__ArrayallKeysForObject (Ref *object)
 Get all keys according to the specified object. More...
 
local allKeysForObject ( local object)
 Get all keys according to the specified object. More...
 
RefobjectForKey (const std::string &key)
 Get the object according to the specified string key. More...
 
RefobjectForKey (intptr_t key)
 Get the object according to the specified integer key. More...
 
local objectForKey ( local key)
 Get the object according to the specified integer key. More...
 
const __StringvalueForKey (const std::string &key)
 Get the value according to the specified string key. More...
 
local valueForKey ( local key)
 Get the value according to the specified string key. More...
 
const __StringvalueForKey (intptr_t key)
 Get the value according to the specified integer key. More...
 
local valueForKey ( local key)
 Get the value according to the specified integer key. More...
 
void setObject (Ref *pObject, const std::string &key)
 Insert an object to dictionary, and match it with the specified string key. More...
 
void setObject (Ref *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...
 
local removeObjectForKey ( local key)
 Remove an object by the specified integer key. More...
 
void removeObjectsForKeys (__Array *pKey__Array)
 Remove objects by an array of keys. More...
 
void removeObjectForElememt (DictElement *pElement)
 Remove an object by an element. More...
 
void removeAllObjects ()
 Remove all objects in the dictionary. More...
 
local removeAllObjects ()
 Remove all objects in the dictionary. More...
 
RefrandomObject ()
 Return a random object in the dictionary. More...
 
bool writeToFile (const char *fullPath)
 Write a dictionary to a plist file. More...
 
virtual void acceptVisitor (DataVisitor &visitor)
var acceptVisitor ( var visitor)
local acceptVisitor ( local visitor)
virtual __Dictionaryclone () const
 returns a copy of the Ref More...
 
- Public Member Functions inherited from Ref
void retain ()
 Retains the ownership. More...
 
void release ()
 Releases the ownership immediately. More...
 
Refautorelease ()
 Releases the ownership sometime soon automatically. More...
 
unsigned int getReferenceCount () const
 Returns the Ref's current reference count. More...
 
local getReferenceCount ()
 Returns the Ref's current reference count. More...
 
virtual ~Ref ()
- Public Member Functions inherited from Clonable
virtual ~Clonable ()
Refcopy () const
 returns a copy of the Ref. More...
 
var copy ()
 returns a copy of the Ref. More...
 
local copy ()
 returns a copy of the Ref. More...
 

Static Public Member Functions

static __Dictionarycreate ()
 Create a dictionary. More...
 
static __DictionarycreateWithDictionary (__Dictionary *srcDict)
 Create a dictionary with an existing dictionary. More...
 
static __DictionarycreateWithContentsOfFile (const char *pFileName)
 Create a dictionary with a plist file. More...
 
static __DictionarycreateWithContentsOfFileThreadSafe (const char *pFileName)
 Create a dictionary with a plist file. More...
 

Public Attributes

DictElement_elements
 All the elements in dictionary. More...
 

Additional Inherited Members

- Protected Member Functions inherited from Ref
 Ref ()
 Constructor. More...
 
 Ref ()
 Constructor. More...
 
- Protected Attributes inherited from Ref
unsigned int _referenceCount
 count of references More...
 
local _referenceCount
 count of references More...
 

Detailed Description

Dictionary is a class like NSDictionary in Obj-C .

Note
Only the pointer of Object or its subclass can be inserted to Dictionary.
// Create a dictionary, return an autorelease object.
// Insert objects to dictionary
String* pValue1 = String::create("100");
String* pValue2 = String::create("120");
Integer* pValue3 = Integer::create(200);
pDict->setObject(pValue1, "key1");
pDict->setObject(pValue2, "key2");
pDict->setObject(pValue3, "key3");
// Get the object for key
String* pStr1 = static_cast<String*>(pDict->objectForKey("key1"));
log("{ key1: %s }", pStr1->getCString());
Integer* pInteger = static_cast<Integer*>(pDict->objectForKey("key3"));
log("{ key3: %d }", pInteger->getValue());

Constructor & Destructor Documentation

The constructor of Dictionary.

The destructor of Dictionary.

Member Function Documentation

virtual void acceptVisitor ( DataVisitor visitor)
virtual
var acceptVisitor ( var  visitor)
virtual
local acceptVisitor ( local  visitor)
virtual
__Array* allKeys ( )

Return all keys of elements.

Returns
The array contains all keys of elements. It's an autorelease object yet.
local allKeys ( )

Return all keys of elements.

Returns
The array contains all keys of elements. It's an autorelease object yet.
__Array* allKeysForObject ( Ref 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.
local allKeysForObject ( local  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 __Dictionary* clone ( ) const
virtual

returns a copy of the Ref

Implements Clonable.

unsigned int count ( )

Get the count of elements in Dictionary.

Returns
The count of elements.
local count ( )

Get the count of elements in Dictionary.

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

Create a dictionary.

Returns
A dictionary which is an autorelease object.
See also
createWithDictionary(Dictionary*), createWithContentsOfFile(const char*), createWithContentsOfFileThreadSafe(const char*).
local create ( )
static

Create a dictionary.

Returns
A dictionary which is an autorelease object.
See also
createWithDictionary(Dictionary*), createWithContentsOfFile(const char*), createWithContentsOfFileThreadSafe(const char*).
static __Dictionary*
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(Dictionary*), createWithContentsOfFileThreadSafe(const char*).
local createWithContentsOfFile ( local  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(Dictionary*), createWithContentsOfFileThreadSafe(const char*).
static __Dictionary*
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 __Dictionary*
createWithDictionary
( __Dictionary 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*).
local createWithDictionary ( local  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*).
bool init ( )

Initializes the dictionary.

It returns true if the initializations was successful.

Ref* 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 String* pointers. Convert it by following code.
String* pStr = static_cast<String*>(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.
String* pStr2 = dynamic_cast<String*>(pDict->objectForKey("key1"));
if (pStr2 != NULL) {
// Do something about pStr2
}
See also
objectForKey(intptr_t)
local objectForKey ( local  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 String* pointers. Convert it by following code.
String* pStr = static_cast<String*>(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.
String* pStr2 = dynamic_cast<String*>(pDict->objectForKey("key1"));
if (pStr2 != NULL) {
// Do something about pStr2
}
See also
objectForKey(intptr_t)
Ref* 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&)
local objectForKey ( local  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&)
Ref* randomObject ( )

Return a random object in the dictionary.

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

Remove an object by an element.

Parameters
pElementThe element need to be removed.
See also
removeObjectForKey(const std::string&), removeObjectForKey(intptr_t), removeObjectsForKeys(__Array*), 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(__Array*), removeObjectForElememt(DictElement*), removeAllObjects().
local removeObjectForKey ( local  key)

Remove an object by the specified string key.

Parameters
keyThe string key for searching.
See also
removeObjectForKey(intptr_t), removeObjectsForKeys(__Array*), removeObjectForElememt(DictElement*), 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(__Array*), removeObjectForElememt(DictElement*), removeAllObjects().
local removeObjectForKey ( local  key)

Remove an object by the specified integer key.

Parameters
keyThe integer key for searching.
See also
removeObjectForKey(const std::string&), removeObjectsForKeys(__Array*), removeObjectForElememt(DictElement*), removeAllObjects().
void removeObjectsForKeys ( __Array pKey__Array)

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(DictElement*), removeAllObjects().
local removeObjectsForKeys ( local  pKey__Array)

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(DictElement*), removeAllObjects().
void setObject ( Ref 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(Ref*, intptr_t)
local setObject ( local  pObject,
local  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(Ref*, intptr_t)
void setObject ( Ref 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(Ref*, const std::string&)
local setObject ( local  pObject,
local  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(Ref*, const std::string&)
const __String* 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 __String pointer.
Parameters
keyThe string key for searching
Returns
An instance of String. It will return an empty string if the objects aren't __String pointer or the key wasn't found.
See also
valueForKey(intptr_t)
local valueForKey ( local  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 __String pointer.
Parameters
keyThe string key for searching
Returns
An instance of String. It will return an empty string if the objects aren't __String pointer or the key wasn't found.
See also
valueForKey(intptr_t)
const __String* 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 __String pointer.
Parameters
keyThe string key for searching.
Returns
An instance of String. It will return an empty string if the objects aren't __String pointer or the key wasn't found.
See also
valueForKey(intptr_t)
local valueForKey ( local  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 __String pointer.
Parameters
keyThe string key for searching.
Returns
An instance of String. It will return an empty string if the objects aren't __String 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

DictElement* _elements

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: