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

原创粉丝点击