You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
string UserDefault::getStringForKey(constchar* pKey, const std::string & defaultValue)
{
constchar* value = nullptr;
tinyxml2::XMLElement* rootNode;
tinyxml2::XMLDocument* doc;
tinyxml2::XMLElement* node;
node = getXMLNodeForKey(pKey, &rootNode, &doc);
// find the nodeif (node && node->FirstChild())
{
value = (constchar*)(node->FirstChild()->Value());
}
string ret = defaultValue;
if (value)
{
ret = string(value);
}
if (doc) delete doc;
return ret;
}
staticvoidsetValueForKey(constchar* pKey, constchar* pValue)
{
tinyxml2::XMLElement* rootNode;
tinyxml2::XMLDocument* doc;
tinyxml2::XMLElement* node;
// check the paramsif (! pKey || ! pValue)
{
return;
}
// find the node
node = getXMLNodeForKey(pKey, &rootNode, &doc);
// if node exist, change the contentif (node)
{
if (node->FirstChild())
{
node->FirstChild()->SetValue(pValue);
}
else
{
tinyxml2::XMLText* content = doc->NewText(pValue);
node->LinkEndChild(content);
}
}
else
{
if (rootNode)
{
tinyxml2::XMLElement* tmpNode = doc->NewElement(pKey);//new tinyxml2::XMLElement(pKey);
rootNode->LinkEndChild(tmpNode);
tinyxml2::XMLText* content = doc->NewText(pValue);//new tinyxml2::XMLText(pValue);
tmpNode->LinkEndChild(content);
}
}
// save file and free docif (doc)
{
doc->SaveFile(FileUtils::getInstance()->getSuitableFOpen(UserDefault::getInstance()->getXMLFilePath()).c_str());
delete doc;
}
}
LocalStorage 的存储与读取
/** sets an item in the LS */voidlocalStorageSetItem( const std::string& key, const std::string& value)
{
assert( _initialized );
int ok = sqlite3_bind_text(_stmt_update, 1, key.c_str(), -1, SQLITE_TRANSIENT);
ok |= sqlite3_bind_text(_stmt_update, 2, value.c_str(), -1, SQLITE_TRANSIENT);
ok |= sqlite3_step(_stmt_update);
ok |= sqlite3_reset(_stmt_update);
if (ok != SQLITE_OK && ok != SQLITE_DONE)
printf("Error in localStorage.setItem()\n");
}
/** gets an item from the LS */boollocalStorageGetItem( const std::string& key, std::string *outItem )
{
assert( _initialized );
int ok = sqlite3_reset(_stmt_select);
ok |= sqlite3_bind_text(_stmt_select, 1, key.c_str(), -1, SQLITE_TRANSIENT);
ok |= sqlite3_step(_stmt_select);
constunsignedchar *text = sqlite3_column_text(_stmt_select, 0);
if (ok != SQLITE_OK && ok != SQLITE_DONE && ok != SQLITE_ROW)
{
printf("Error in localStorage.getItem()\n");
returnfalse;
}
elseif (!text)
{
returnfalse;
}
else
{
outItem->assign((constchar*)text);
returntrue;
}
}
The text was updated successfully, but these errors were encountered:
cocos 有两种本地存储方式, 一种是UserDefault 另一种是 LocalStorage。
其中UserDefault是基于xml, 而LocalStorage 是基于的sqlite。
至于为什么要搞出两套方案,应该是jsb没办法去使用UserDefault,只能用LocalStorage。
对于使用层面上来说,两者其实拿到的都是字符串类型,然后需要根据自己的需要去进行转换成想要的数据类型,UserDefault封装了几种常用的类型,而LocalStorage是没有封装的.
最后欣赏一下代码
UserDefault 的 字符串 数据的存储读取
LocalStorage 的存储与读取
The text was updated successfully, but these errors were encountered: