// Create a CCDictionary object CCDictionary* dict = CCDictionary::create(); // Add a key-value pair to the dictionary dict->setObject(CCInteger::create(5), "score"); // Retrieve the value using the key CCInteger* score = (CCInteger*)dict->objectForKey("score"); int playerScore = score->getValue(); // playerScore will be 5
// Create a CCDictionary object CCDictionary* dict = CCDictionary::create(); // Add multiple key-value pairs to the dictionary dict->setObject(CCInteger::create(5), "score"); dict->setObject(CCString::create("Player"), "name"); dict->setObject(CCFloat::create(2.5f), "time"); // Loop through the dictionary and print each key-value pair CCDictElement* element = nullptr; CCDICT_FOREACH(dict, element) { CCString* key = element->getStrKey(); CCObject* value = element->getObject(); if (CCInteger* intValue = dynamic_castThis example demonstrates how to add multiple key-value pairs to a CCDictionary object, loop through the dictionary, and print each key-value pair. It also shows how to use dynamic_cast to retrieve the value of each key.(value)) { int intValue = intValue->getValue(); CCLOG("Key: %s, Value: %d", key->getCString(), intValue); } else if (CCString* strValue = dynamic_cast (value)) { CCLOG("Key: %s, Value: %s", key->getCString(), strValue->getCString()); } else if (CCFloat* floatValue = dynamic_cast (value)) { float floatValue = floatValue->getValue(); CCLOG("Key: %s, Value: %f", key->getCString(), floatValue); } }