我有下面的代碼應在更新NSUserDefaults的值:如何更新NSUserDefault
- (id) createBoardWithTitle:(NSString *)pTitle withScores:(NSArray *)pScores andNames:(NSArray *)pNames andDisplayStrings:(NSArray *)pStrings orderBy:(NSString *)pOrder ofType:(NSString *)pType
{
if((self == [super init]))
{
boardEntries = [NSMutableArray arrayWithCapacity:10];
// Build the data
for(...){
// populate boardEntries
}
// create an Dictionary to save
NSDictionary *savedData = [NSDictionary dictionaryWithObjectsAndKeys:pType, @"type", pOrder, @"order", boardEntries, @"entries", nil];
// Load the old boards
NSDictionary *existingBoards = [[NSUserDefaults standardUserDefaults] objectForKey:@"BlockDepotLeaderboards"];
// Create a mutable dictionary to replace the old immutable dictionary
NSMutableDictionary *newBoards = [NSMutableDictionary dictionaryWithCapacity:[existingBoards count]+1];
// transfer the old dictionary into the new dictionary
[newBoards addEntriesFromDictionary:existingBoards];
// add the new board to the new dictionary
[newBoards setObject:savedData forKey:pTitle];
// check to make sure it looks like it should by eye
NSLog(@"New Boards: %@", newBoards);
// Replace the old date in NSUserdefaults
[[NSUserDefaults standardUserDefaults] setObject:newBoards forKey:@"BlockDepotleaderboards"];
// Update: Do I really need to call this?
[[NSUserDefaults standardUserDefaults] synchronize];
// Check to make sure it looks as it should by eye
NSLog(@" Defaults--- %@", [[NSUserDefaults standardUserDefaults] objectForKey:@"BlockDepotLeaderboards"]);
}
return self;
}
至於我可以告訴大家,這是相關代碼。或許它可能比它更「單調」。但據我所知,從NSUserDefaults返回的任何東西都是不可變的,所以我必須重新創建它作爲一個可變的對象,添加需要添加的東西,然後替換NSUserDefaults中的對象。而且我認爲我上面的嘗試應該是有效的。
的輸出的NSLog(@ 「的新版塊:%@」,newBoards)ID
New Boards: {
"Marathon: Times" = {
entries = (
{
displayString = "10:00";
name = "Tom Elders";
score = 600;
},
{
displayString = "10:30";
name = "A.R. Elders";
score = 630;
},
{
displayString = "11:00";
name = "L. Lancaster-Harm";
score = 660;
},
// and so on.....
);
order = ASC;
type = TIMES;
};
String = Test;
}
「字符串=測試」 只是一個測試項,這樣在AppDelegate.m文件中設置:
if(![[NSUserDefaults standardUserDefaults] objectForKey:@"BlockDepotLeaderboards"]){
NSMutableDictionary *leadboardHolder = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"Test", @"String", nil];
[[NSUserDefaults standardUserDefaults] setObject:leadboardHolder forKey:@"BlockDepotLeaderboards"];
[[NSUserDefaults standardUserDefaults] synchronize];
}else{
NSLog(@"Leaderboards Dict Exists %@", [NSUserDefaults standardUserDefaults]);
}
所以我知道我在之後肯定是有。我只是知道這將是我錯過的一些愚蠢的東西。但是我看不到它或者搞清楚了。
任何人都可以看到我搞砸了嗎?
不要做'objectForKey:@「BlockDepotLeaderboards」'。你不知道返回的對象實際上是什麼。使用-dictionaryForKey:來代替,並通過創建一個新字典來處理它爲零的情況。 – 2009-12-12 17:13:00
如果您正在訪問NSUserDefaults的值,您應該使用dictionForKey:或將該對象的類型轉換爲字典 – 2009-12-13 07:39:17