cocos2d-x学习(6)----CCUserDefault(XML数据)

1.CCUserDefault:

CCUserDefault 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

CCUserDefault  是一个简单的数据库,它支持bool, int, float, double, string 5种数据的处理。  如果,你懂得Android的sharedpreferences,又你就完全理解这个类的作用!

2.关键的方法:

bool 	getBoolForKey (const char *pKey)

 	Get bool value by key, if the key doesn't exist, a default value will return. 

bool 	getBoolForKey (const char *pKey, bool defaultValue)

 
int 	getIntegerForKey (const char *pKey)

 	Get integer value by key, if the key doesn't exist, a default value will return. 

int 	getIntegerForKey (const char *pKey, int defaultValue)

 
float 	getFloatForKey (const char *pKey)

 	Get float value by key, if the key doesn't exist, a default value will return. 

float 	getFloatForKey (const char *pKey, float defaultValue)

 
double 	getDoubleForKey (const char *pKey)

 	Get double value by key, if the key doesn't exist, a default value will return. 

double 	getDoubleForKey (const char *pKey, double defaultValue)

 
std::string 	getStringForKey (const char *pKey)

 	Get string value by key, if the key doesn't exist, a default value will return. 

std::string 	getStringForKey (const char *pKey, const std::string &defaultValue)

 
void 	setBoolForKey (const char *pKey, bool value)

 	Set bool value by key. 

void 	setIntegerForKey (const char *pKey, int value)

 	Set integer value by key. 

void 	setFloatForKey (const char *pKey, float value)

 	Set float value by key. 

void 	setDoubleForKey (const char *pKey, double value)

 	Set double value by key. 

void 	setStringForKey (const char *pKey, const std::string &value)

 	Set string value by key. 

 
void 	flush ()

 	Save content to xml file. 


static CCUserDefault * 	sharedUserDefault ()
static void 	purgeSharedUserDefault ()

static const std::string & 	getXMLFilePath ()
static bool 	isXMLFileExist ()

3.代码样例:

// change the value
CCUserDefault::sharedUserDefault()->setStringForKey("string", "value2");
CCUserDefault::sharedUserDefault()->setIntegerForKey("integer", 11);
CCUserDefault::sharedUserDefault()->setFloatForKey("float", 2.5f);
CCUserDefault::sharedUserDefault()->setDoubleForKey("double", 2.6);
CCUserDefault::sharedUserDefault()->setBoolForKey("bool", false);

CCUserDefault::sharedUserDefault()->flush();

// print value
ret = CCUserDefault::sharedUserDefault()->getStringForKey("string");
CCLOG("string is %s", ret.c_str());
d = CCUserDefault::sharedUserDefault()->getDoubleForKey("double");
CCLOG("double is %f", d);
i = CCUserDefault::sharedUserDefault()->getIntegerForKey("integer");
CCLOG("integer is %d", i);
f = CCUserDefault::sharedUserDefault()->getFloatForKey("float");
CCLOG("float is %f", f);
b = CCUserDefault::sharedUserDefault()->getBoolForKey("bool");


参考:

1.http://www.cocos2d-x.org/reference/native-cpp/d0/d79/classcocos2d_1_1_c_c_user_default.html#ae11ba15de3458c8c7683c16c279fce16

2.http://blog.sina.com.cn/s/blog_61d2d3f50100x29x.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hfreeman2008

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值