How to write in a CCDictionary using keys

Hi.

I’m trying to read a value from a dictionary, and write it again. My plist has a lot of values, but i only want to edit 1 of them.

const char dictionary = “myPlist.plist”; // plist
const char
miniDic = “Test”;

// Get Path
const char *plistPath = myFileUtils~~>fullPathFromRelativePath; // path
// Create Dictionary with Path
CCDictionary plistDictionary = CCDictionary::create;
// check if exist Dictionary in path
if
{
CCLog;
}
// Open Dictionary
CCDictionary
miniDictionary = plistDictionary~~>objectForKey(std::string(miniDic));

// Load player stadistic from memory
int myPlayedTimeTotal = playerStadistics~~>getTimetotal; // Result: “70” // Work
// Load value from plist
int myPlayedTimeTotalMax = atoi)~~>getCString()); // Result: “0” //Work

if (myPlayedTimeTotal > myPlayedTimeTotalMax)
{
// GO INSIDE, 70 > 0
std::stringstream out;
out << myPlayedTimeTotal;
miniDictionary~~>setObject),std::string); // Want to store the value in the dictionary
myPlayedTimeTotalMax = atoi)~~>getCString()); // Result: “0” ; It doesn’t do it!
}

What i missing? Exist another way to do it easier?

i tried, it work
@ std::stringstream out;
out << 70;
CCString str = out.str();
dict~~>setObject;
if)
{
CCString *str2 =dict~~>objectForKey(“key”);
CCLOG( “Str(s) Int(d)”,str2~~>getCString,str2~~>intValue());
}
@
maybe you have problem in line code:
miniDictionary->setObject(&CCString(out.str()),std::string("timetotal_max"));
i have error of temporary object

Yeah, thanks, but the changes don´t change the plists. Is necessary to push the new values???

In CCUserDefault you must to flush the changes.

You can’t change plist only change dictionary

Is not possible to store the new values in the plist???

That is what i want to do. I´m trying to store data in some plist to save stadistics and achievements

You can make saving plist manually in documents and then they load plist.
i do so:
//load plist
`std::string path = CCFileUtils::sharedFileUtils()->getDocumentPath();
path.append(_filename);
CCDictionary *dict = NULL;
std::ifstream ifile(path.c_str());

if (ifile)// The file exists, and is open for input
{
        CCDictionary *dict1 = CCDictionary::createWithContentsOfFile(path.c_str());
        std::ofstream out(path.c_str(), std::ios::in | std::ios::binary);
        dict = CCDictionary::createWithContentsOfFile(_filename);

}
else
{
dict = CCDictionary::createWithContentsOfFile(_filename);
};

//save plist
std::string path = CCFileUtils::sharedFileUtils()->getDocumentPath();
path.append(_filename);
std::ofstream out(path.c_str(), std::ios::out | std::ios::binary);
if (out.is_open())
{
out << “<?xml version=\"1.0\" encoding=\"UTF-8\"?>”;
out << “<plist version=“1.0”>”;
out << “version”<< version <<"";
…`

i try again
//i’m add this method to CCFileUtils
std::string CCFileUtils::getDocumentPath() { // save to document folder NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; std::string strRet = [documentsDirectory UTF8String]; strRet.append("/"); return strRet; }

Hi.

I’m trying to save the file manually. The problem is, my plist has 15 dictionaries, 1 per level. To store all correctly, i must to read all of them and store it. I would like to avoid all the operation, but CCFileUtils and CCDictionary don’t allow to flush changes.

Any Idea?

Sorry, I’m wrong. I have a dictionary with all of them.

Then, I create a ofstream to put all data. Just like you posted:

/////

if (out.is_open())
{
out << “<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE plist PUBLIC -//Apple//DTD PLIST 1.0//EN ttp://www.apple.com/DTDs/PropertyList-1.0.dtd>”;
out << “<plist version=.0><dict>”;
out << “<key>version</key><string>”<< version <<“</string>”;

/////

But, How could I put the dictionary all the info from my modified dictionary into the out ofstream?

Maybe I do not understand you, but I do so:
creating a structure (with the required data);
load a dictionary in this structure;
now only works with this structure(this is faster than dictionary);
keep the structure as a dictionary in plist
//in header
struct Level
{
int score;
int time;
};
std::list m_list_lvl;

//load
CCArray* lvlArray = (CCArray**)dict_world->objectForKey;
CCObject** plvlObj = NULL;
CCARRAY_FOREACH(lvlArray, plvlObj)
{
CCDictionary* dict_lvl = (CCDictionary*)(plvlObj);
Level *lvl;
*lvl.time = dictInt(dict_lvl, “time”);
_lvl.score = dictInt(dict_lvl, “score”);
m_list_lvl.push_back(_lvl);
};

//find and change structure
…….
//save structure

sorry i have bad english

Thanks for your help. It is not possible to store data from a CCDictionary to plist directly. I think I will try using libXML2, that work on Android and iOS. Thanks for your help.

Pipero Man wrote:

Hi.
>
I’m trying to read a value from a dictionary, and write it again. My plist has a lot of values, but i only want to edit 1 of them.
>
>
const char dictionary = “myPlist.plist”; // plist
const char
miniDic = “Test”;
>
// Get Path
const char *plistPath = myFileUtils~~>fullPathFromRelativePath; // path
>
// Create Dictionary with Path
CCDictionary plistDictionary = CCDictionary::create;
>
// check if exist Dictionary in path
if
{
CCLog;
}
>
// Open Dictionary
CCDictionary
miniDictionary = plistDictionary~~>objectForKey(std::string(miniDic));
>
// Load player stadistic from memory
int myPlayedTimeTotal = playerStadistics~~>getTimetotal; // Result: “70” // Work
>
// Load value from plist
int myPlayedTimeTotalMax = atoi)>getCString()); // Result: “0” //Work
>
if (myPlayedTimeTotal > myPlayedTimeTotalMax)
{
// GO INSIDE, 70 > 0
std::stringstream out;
out << myPlayedTimeTotal;
miniDictionary
>setObject),std::string); // Want to store the value in the dictionary
myPlayedTimeTotalMax = atoi)~~>getCString()); // Result: “0” ; It doesn’t do it!
}
>
What i missing? Exist another way to do it easier?

convert your plist into nsdata using nspropertlistserialisation and then write the data to dictionary