cocos2d-x  3.0alpha1
 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:
Object 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 (Object *object)
 Get all keys according to the specified object. More...
 
local allKeysForObject ( local object)
 Get all keys according to the specified object. More...
 
ObjectobjectForKey (const std::string &key)
 Get the object according to the specified string key. More...
 
ObjectobjectForKey (intptr_t 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 (Object *pObject, const std::string &key)
 Insert an object to dictionary, and match it with the specified string key. More...
 
void setObject (Object *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 (Array *pKeyArray)
 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...
 
ObjectrandomObject ()
 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)
virtual Dictionaryclone () const
 returns a copy of the object More...
 
- Public Member Functions inherited from Object
 Object ()
 Constructor. More...
 
virtual ~Object ()
void release ()
 Release the ownership immediately. More...
 
void retain ()
 Retains the ownership. More...
 
Objectautorelease ()
 Release the ownership sometime soon automatically. More...
 
bool isSingleReference () const
 Returns a boolean value that indicates whether there is only one reference to the object. More...
 
unsigned int retainCount () const
 Returns the object's current reference count. More...
 
virtual bool isEqual (const Object *object)
 Returns a boolean value that indicates whether this object and a given object are equal. More...
 
virtual void update (float dt)
- Public Member Functions inherited from Clonable
virtual ~Clonable ()
Objectcopy () const
 returns a copy of the object. More...
 
var copy ()
 returns a copy of the object. More...
 
local copy ()
 returns a copy of the object. More...
 

Static Public Member Functions

static Dictionarycreate ()
 Create a dictionary. More...
 
local create ()
 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...
 
- Public Attributes inherited from Object
unsigned int _ID
 object id, ScriptSupport need public _ID More...
 
int _luaID
 Lua reference id. More...
 

Additional Inherited Members

- Protected Attributes inherited from Object
unsigned int _reference
 count of references More...
 
unsigned int _autoReleaseCount
 count of autorelease 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 = (String*)pDict->objectForKey("key1");
log("{ key1: %s }", pStr1->getCString());
Integer* pInteger = (Integer*)pDict->objectForKey("key3");
log("{ key3: %d }", pInteger->getValue());

Constructor & Destructor Documentation

The constructor of Dictionary.

~Dictionary ( )

The destructor of Dictionary.

Member Function Documentation

virtual void acceptVisitor ( DataVisitor visitor)
virtual

Reimplemented from Object.

var acceptVisitor ( var  visitor)
virtual

Reimplemented from Object.

local acceptVisitor ( local  visitor)
virtual

Reimplemented from Object.

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 ( Object 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 object

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.

Object* 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 = (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 = (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)
Object* 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&)
Object* 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 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(DictElement*), removeAllObjects().
local removeObjectsForKeys ( local  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(DictElement*), removeAllObjects().
void setObject ( Object 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(Object*, 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(Object*, intptr_t)
void setObject ( Object 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(Object*, 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(Object*, 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: