UserDefault的使用

   // ①设置用户数据

    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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值