UserDefault的使用
来源:互联网 发布:ipad看书软件 编辑:程序博客网 时间:2024/06/10 09:02
// ①设置用户数据
UserDefault::getInstance()->setStringForKey("string", "value1");
UserDefault::getInstance()->setIntegerForKey("integer", 10);
UserDefault::getInstance()->setFloatForKey("float", 2.3f);
UserDefault::getInstance()->setDoubleForKey("double", 2.4);
UserDefault::getInstance()->setBoolForKey("bool", true);
log("********************** 第一次设置的用户数***********************");
// ②打印之前设置的数据值
std::string ret = UserDefault::getInstance()->getStringForKey("string");
log("string is %s", ret.c_str());
double d = UserDefault::getInstance()->getDoubleForKey("double");
log("double is %f", d);
int i = UserDefault::getInstance()->getIntegerForKey("integer");
log("integer is %d", i);
float f = UserDefault::getInstance()->getFloatForKey("float");
log("float is %f", f);
bool b = UserDefault::getInstance()->getBoolForKey("bool");
if (b)
{
log("bool is true");
}
else
{
log("bool is false");
}
// ③通过Key修改用户数据
UserDefault::getInstance()->setStringForKey("string", "value2");
UserDefault::getInstance()->setIntegerForKey("integer", 11);
UserDefault::getInstance()->setFloatForKey("float", 2.5f);
UserDefault::getInstance()->setDoubleForKey("double", 2.6);
UserDefault::getInstance()->setBoolForKey("bool", false);
// ④将XML数据保存到文件中。
UserDefault::getInstance()->flush();
log("********************** 修改之后的用户数据 ***********************");
// ⑤再次获取用户数据并打印
ret = UserDefault::getInstance()->getStringForKey("string");
log("string is %s", ret.c_str());
d = UserDefault::getInstance()->getDoubleForKey("double");
log("double is %f", d);
i = UserDefault::getInstance()->getIntegerForKey("integer");
log("integer is %d", i);
f = UserDefault::getInstance()->getFloatForKey("float");
log("float is %f", f);
b = UserDefault::getInstance()->getBoolForKey("bool");
if (b)
{
log("bool is true");
}
else
{
log("bool is false");
}
// ⑥获取XML文件的默认地址
std::string filePath = UserDefault::getInstance()->getXMLFilePath();
log("XML文件保存路径:%s",filePath.c_str());
UserDefault是个微型数据库,你可以将基础数据类型存储在里面或从里面读取出来.
例如:setBoolForKey("played", true)是将一个bool值存储进去,其key是"played",因此你可以通过getBoolForKey("played")从数据库中读取该bool值,其支持的基础数据类型如下: bool, int, float, double, string
static void destroyInstance()
static void flush() //将内容保存到xml文件
bool getBoolForKey(const char * pKey)
读取bool值,如果这个key不存在,将返回默认值 你可以设置默认值,或不设置则为false
bool getBoolForKey(const char * pKey,bool defaultValue )
Data getDataForKey(const char * pKey)
读取基于key的二进制数据(binary data),如果这个key不存在,将返回默认值 你可以设置默认值,或不设置则为null.
Data getDataForKey (const char * pKey,const Data & defaultValue )
double getDoubleForKey (const char * pKey)
读取double值,如果这个key不存在,将返回默认值 你可以设置默认值,或不设置则为0.0.
double getDoubleForKey (const char * pKey,double defaultValue )
float getFloatForKey (const char * pKey)
读取float值,如果这个key不存在,将返回默认值 你可以设置默认值,或不设置则为0.0f.
float getFloatForKey (const char *pKey,float defaultValue )
static UserDefault* getInstance()
返回singleton
int getIntegerForKey (const char * pKey)
读取integer值,如果这个key不存在,将返回默认值 你可以设置默认值,或不设置则为0
int getIntegerForKey (const char * pKey,int defaultValue )
std::string getStringForKey(const char * pKey)
读取string值,如果这个key不存在,将返回默认值 你可以设置默认值,或不设置则为"".
std::string getStringForKey(const char * pKey,const std::string & defaultValue )
static const std::string& getXMLFilePath()
static bool isXMLFileExist ()
void setBoolForKey (const char * pKey,bool value )
存储bool值
void setDataForKey (const char * pKey,const Data & value )
存储二进制数据(binary data)
void setDoubleForKey(const char * pKey,double value )
存储double值
void setFloatForKey(const char * pKey,float value )
存储float值
void setIntegerForKey (const char * pKey,int value )
存储integer值
void setStringForKey (const char * pKey,const std::string & value )
存储string值
- UserDefault的使用
- UserDefault的使用
- UserDefault 使用详解
- Cocos2d-x3.2下Lua,UserDefault、Texture的使用
- iOS 本地数据持久化 中UserDefault的使用
- iOS中userdefault的使用[转]
- UserDefault
- UserDefault
- UserDefault
- UserDefault
- UserDefault
- UserDefault
- cocos2d-x3.2 的UserDefault
- Mac下userdefault的路径
- 使用CCUserDefault 判断用户是否是第一次登陆系统及UserDefault全路径的获取
- Cocos2d-x3.2 UserDefault用户数据 使用
- [cocos2dx笔记016]cocos2dx 3.2 的UserDefault
- Cocos2d-x使用UserDefault数据持久化实例
- Text的使用
- OpenStack vs VMware
- 各领域公开数据集下载
- 手把手教你快速实现Android底部导航栏
- java 抽象 abstract关键字
- UserDefault的使用
- 《代码不朽》书籍分享
- webpack 安装时报错 eintegrity
- ResNet
- 应用内存onLowMemory& onTrimMemory优化
- HDU-1233-还是畅通工程
- webpack打包报javaScript heap out of memory的解决方法
- 粒子特效
- 掌握Chrome开发工具:新一代前端开发技术