PLIST handling in cocos2d-x
In cocos2d-x, PLIST file can be more easily used than normal XML files as following:
FileUtils* util= FileUtils::getInstance();
std::string path= util->fullPathForFilename("res/test.plist");
ValueMap map= util->getValueMapFromFile(path);
for (auto element : map) {
std::string key= element.first;
Value value= element.second;
switch (value.getType()) {
case Value::Type::BOOLEAN:
CCLOG("%s, %s", key.c_str(), value.asBool()? "true" : "false");
break;
case Value::Type::INTEGER:
CCLOG("%s, %d", key.c_str(), value.asInt());
break;
case Value::Type::FLOAT:
CCLOG("%s, %f", key.c_str(), value.asFloat());
break;
case Value::Type::DOUBLE:
CCLOG("%s, %f", key.c_str(), value.asDouble());
break;
case Value::Type::STRING:
CCLOG("%s, %s", key.c_str(), value.asString().c_str());
break;
default:
break;
}
}
PLIST’s data containers are Array and Dictionary . Array is std::vector<Value> and Dictionary is std::unordered_map<std::string, Value> .
The Value types also includes VECTOR , MAP , and INT_KEY_MAP each of which corresponds to the types of ValueVector , ValueMap , and ValueMapIntKey and they are defined as following:
typedef std::vector<Value> ValueVector; typedef std::unordered_map<std::string, Value> ValueMap; typedef std::unordered_map<int, Value> ValueMapIntKey;
To store data in PLIST:
ValueMap map;
for (int i= 0; i < 10; i++)
{
std::string key= StringUtils::format("key_%d", i);
Value val= Value(i);
map.insert(std::make_pair(key, val));
}
std::string fullpath= util->getWritablePath() + "/test.plist";
FileUtils::getInstance()->writeToFile(map, fullpath);