를 업데이트하는 방법 내가 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에서 반환 된 모든 내용은 변경되지 않으므로 Mutable 개체로 다시 만들어야하고 NSUserDefaults에서 개체를 추가해야하는 항목을 추가해야합니다. 그리고 저는 위에서 시도한 것이 효과가 있다고 생각합니다.
(@ "새 보드 %의 @", newBoards) NSLog의 출력 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 :를 사용하고 새로운 사전을 작성하여 nil 인 경우를 처리하십시오. –
NSUserDefaults에서 값에 액세스하는 경우 dictionForKey :를 사용하거나 사전에 객체를 캐스팅해야합니다 –