I read through all of the posts here on user defaults and one asked about saving a dictionary.
Here’s what my table view looks like in the form that I would put it as a property:
property players: {{"Doc", {}, ""}, {"Grumpy", {}, ""}, {"Sneezy", {}, ""}, {"Sleepy", {}, ""}, {"Happy", {}, ""}, {"Bashful", {}, ""}, {"Dopey", {}, ""}
}
So it’s pretty simple, seven (or more) rows containing a name, a list of strings, and a string.
I made a property list manually and it looks like this as output of “defaults read” in Terminal:
{
“Player 1” = {
Name = Grumpy;
VotedFor = Dopey;
VotesAgainst = (
Sneezy,
Dopey,
Bashful
);
};
“Player 2” = {
Name = Dopey;
VotedFor = Doc;
VotesAgainst = (
Doc,
Sleepy
);
};
“Player 3” = {
Name = Sneezy;
VotedFor = Bashful;
VotesAgainst = (
Grumpy,
Bashful
);
};
}
I tried the following code to save it as user defaults:
to saveData()
tell theDataSource
repeat with theRow in data rows
tell theRow
make new default entry at end of default entries of user defaults with properties {name:contents of data cell "playernames", votedFor:contents of data cell "currentVotedPlayer", VotesAgainst:contents of data cell "votersFor"}
end tell
end repeat
end tell
end saveData
Of course it’s giving me a runtime error.
Will user defaults take a dictionary like this and read and write it properly? I saw the example with the Beatles and that is an array but this dictionary has one more level.
I hate to have to script a custom data format just to save and load a small dictionary.
Can anyone tell me if I am on the right track? Will user defaults save and load this kind of object? Maybe I am screwing up something by trying to write it a row at a time. I’m not really sure what a user defaults entry is supposed to correspond to when you have arrays within a record.
Any help appreciated.