在Cocos2d-x存储数据使用的类是UserDefault类,下面分析下该类的使用
//.h #include "base/CCPlatformMacros.h" #include <string> #include "base/CCData.h" NS_CC_BEGIN /** * @addtogroup data_storage * @{ */ /** * UserDefault acts as a tiny database. You can save and get base type values by it. * For example, setBoolForKey("played", true) will add a bool value true into the database. * Its key is "played". You can get the value of the key by getBoolForKey("played"). * * It supports the following base types: * bool, int, float, double, string */ class CC_DLL UserDefault { public: // get value methods /** @brief Get bool value by key, if the key doesn't exist, a default value will return. You can set the default value, or it is false. * @js NA */ bool getBoolForKey(const char* pKey); /** * @js NA */ bool getBoolForKey(const char* pKey, bool defaultValue); /** @brief Get integer value by key, if the key doesn't exist, a default value will return. You can set the default value, or it is 0. * @js NA */ int getIntegerForKey(const char* pKey); /** * @js NA */ int getIntegerForKey(const char* pKey, int defaultValue); /** @brief Get float value by key, if the key doesn't exist, a default value will return. You can set the default value, or it is 0.0f. * @js NA */ float getFloatForKey(const char* pKey); /** * @js NA */ float getFloatForKey(const char* pKey, float defaultValue); /** @brief Get double value by key, if the key doesn't exist, a default value will return. You can set the default value, or it is 0.0. * @js NA */ double getDoubleForKey(const char* pKey); /** * @js NA */ double getDoubleForKey(const char* pKey, double defaultValue); /** @brief Get string value by key, if the key doesn't exist, a default value will return. You can set the default value, or it is "". * @js NA */ std::string getStringForKey(const char* pKey); /** * @js NA */ std::string getStringForKey(const char* pKey, const std::string & defaultValue); /** @brief Get binary data value by key, if the key doesn't exist, a default value will return. You can set the default value, or it is null. * @js NA * @lua NA */ Data getDataForKey(const char* pKey); /** * @js NA * @lua NA */ Data getDataForKey(const char* pKey, const Data& defaultValue); // set value methods /** @brief Set bool value by key. * @js NA */ void setBoolForKey(const char* pKey, bool value); /** @brief Set integer value by key. * @js NA */ void setIntegerForKey(const char* pKey, int value); /** @brief Set float value by key. * @js NA */ void setFloatForKey(const char* pKey, float value); /** @brief Set double value by key. * @js NA */ void setDoubleForKey(const char* pKey, double value); /** @brief Set string value by key. * @js NA */ void setStringForKey(const char* pKey, const std::string & value); /** @brief Set binary data value by key. * @js NA * @lua NA */ void setDataForKey(const char* pKey, const Data& value); /** @brief Save content to xml file * @js NA */ void flush(); /** returns the singleton * @js NA * @lua NA */ static UserDefault* getInstance(); /** * @js NA */ static void destroyInstance(); /** deprecated. Use getInstace() instead * @js NA * @lua NA */ CC_DEPRECATED_ATTRIBUTE static UserDefault* sharedUserDefault(); /** * @js NA */ CC_DEPRECATED_ATTRIBUTE static void purgeSharedUserDefault(); /** * @js NA */ const static std::string& getXMLFilePath(); /** * @js NA */ static bool isXMLFileExist(); private: UserDefault(); ~UserDefault(); static bool createXMLFile(); static void initXMLFilePath(); static UserDefault* _userDefault; static std::string _filePath; static bool _isFilePathInitialized; }; // end of data_storage group /// @} NS_CC_END
使用时在自定义类的.cpp文件中添加UserDefault.h,使用离子如下:
//.cpp #include "base/CCUserDefault.h UserDefault::getInstance()->setStringForKey("key", "value"); auto value = UserDefault::getInstance()->getStringForKey("key","Default value"); log("key = %s",value.c_str());
运行结果如下图:控制台打印出 key = value;
时间: 2024-10-05 09:35:22