This blog is generated by the following questions-(code 1):
STD:: String str =FileUtils:: getinstance ()Getwritablepath (); Cclog (Str.c_str ());Userdefault:: getinstance ()Setstringforkey ("Keyqq11","Value11");FileUtils:: getinstance ()Setwritablepath ("d:/"); str =FileUtils:: getinstance ()Getwritablepath (); Cclog (Str.c_str ());Userdefault:: getinstance ()Setstringforkey ("Keyqq12","Value22");
At first I thought Userdefault.xml would spawn in two places, and I was wrong. Userdefault.xml only exists in the default path: c:/users/g40-70/appdata/local/canimal/. Note: For the default path, we can print using the method mentioned above. So I went into a long search for answers – my approach is source debugging. Note: If you are using Cocosstudio project, you need to switch to COCOS2DX project, because Cocosstudio does not provide fileutils implementation. Let's look at the Userdefault::getinstance interface first-(Code 2):
UserDefault* UserDefault::getInstance(){ if (!_userDefault) { initXMLFilePath(); // only create xml file one time // the file exists after the program exit if ((!&& (!createXMLFile())) { return nullptr; } =new (std::nothrow) UserDefault(); } return _userDefault;}
Take a closer look at the Initxmlfilepath () interface-(Code 3):
voidUserDefault::initXMLFilePath(){ if (! _isFilePathInitialized) { FileUtils::getInstance()->getWritablePath() + XML_FILE_NAME; true; } }
As you can see from the above two pieces of code, the Userdefault is set for the path when it is initialized, and the _filepath value is modified only the first time. So the above code 1 problem solved: Because in code 1 we call a total of two times the following code:
UserDefault::getInstance()->setStringForKey(......);
The Userdefault.xml path is set only on the first call, so the second call, although Fileutils._writablepath has been modified at this time, However, it does not make the Userdefault.filepath modification, so code 1 appears only in one place to generate the userdefault.xml.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Independent Developer ER cocos2d-x combat 007" using COCOS2DX Userdefault.xml