编辑,回答:
相当简单,本来应该发现这个:NSUserDefaults
似乎仍然需要Cocoa属性列表对象才能工作,所以问题是在字典中使用常规的Swift Ints
。它们仍然需要是适当的对象(显然)并包裹在NSNumber
中。
标题所说的几乎是什么。 我有一个由三个字典组成的数组用于savedGamesData ...
savedGames[savedGameSlot] = currentSavedGame
userDefaults.setObject(savedGames, forKey: "savedGames")
println("Saving savedGames: \(savedGames)")
userDefaults.synchronize()
println(userDefaults.dictionaryRepresentation())
savedGames是字典数组,currentSavedGame是更新的字典,userDefaults是.standardUserDefaults
第一个println
向我显示数据已按我的预期更改。请注意第二个字典:
Saving savedGames: [
[score: 0, greatestTournamentReached: 1, goats: 0, match: 1, cash: 25, lastTournamentPlayed: 1, tournament: 1, gamesPlayed: 6, batSize: 1, greatestMatchReached: 2, bats: 2],
[score: 0, greatestTournamentReached: 1, goats: 0, match: 2, cash: 11, lastTournamentPlayed: 1, tournament: 1, gamesPlayed: 2, batSize: 1, greatestMatchReached: 3, bats: 3],
[score: 0, greatestTournamentReached: 1, goats: 0, match: 1, cash: 0, lastTournamentPlayed: 1, tournament: 1, gamesPlayed: 0, batSize: 1, greatestMatchReached: 1, bats: 3]]
但是,当我在同步后打印userDefaults 时,尚未存储更改:
savedGames: (
{
batSize = 1;
bats = 2;
cash = 25;
gamesPlayed = 6;
goats = 0;
greatestMatchReached = 2;
greatestTournamentReached = 1;
lastTournamentPlayed = 1;
match = 1;
score = 0;
tournament = 1;
},
{ // This is the one that should have been changed
batSize = 1;
bats = 3;
cash = 1;
gamesPlayed = 1;
goats = 0;
greatestMatchReached = 1;
greatestTournamentReached = 1;
lastTournamentPlayed = 1;
match = 1;
score = 0;
tournament = 1;
},
{
batSize = 1;
bats = 3;
cash = 0;
gamesPlayed = 0;
goats = 0;
greatestMatchReached = 1;
greatestTournamentReached = 1;
lastTournamentPlayed = 1;
match = 1;
score = 0;
tournament = 1;
}