2010-12-01 80 views
0

我遇到NSUserdefaults问题。当我在文本框中输入名称并单击高分时,应用程序将不会响应任何内容,并且键盘仍将保留在屏幕上。当我试图加载数据时,它说名称的数据是零。我的得分是90.有人可以告诉我我的编码有什么问题。数据不保存在NSUserDefaults中

非常感谢。

-(IBAction)savehighscore_button { 


    int i, ii = -1; 

    struct high_score { 
     NSString *name; 
     int highScore; 
    }; 

    struct high_score structArray[10]; 

    NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults]; 

    for (i=0; i<10; i++) { 

     if ([userPreferences stringForKey :[NSString stringWithFormat:@"highScoreNameEntry%d",i]]!=nil && [userPreferences stringForKey :[NSString stringWithFormat:@"highScoreEntry%d"]]!=nil) { 
     structArray[i].name= [userPreferences stringForKey:[NSString stringWithFormat:@"highScoreNameEntry%d",i]]; 
      structArray[i].highScore = [userPreferences integerForKey:[NSString stringWithFormat:@"highScoreEntry%d",i]]; 
      ii = i; 
     } 
    } 
    if (myScore >0) { 
     for (i==ii; i>=0; i--) { 
      if (myScore > structArray[i].highScore) { 
       if (i<9) { 
        structArray[i+1] = structArray[i]; 
        structArray[i].name = nametextbox.text; 
        structArray[i].highScore = myScore; 

        if (i==ii && i<9) { 
         structArray[i+1].name = nametextbox.text; 
         structArray[i+1].highScore = myScore; 
         ii=i+i; 
        } 

        else if(i==ii && i<9) { 
         structArray[i+1].name = nametextbox.text; 
         structArray[i+1].highScore = myScore; 
         ii=i+1; 
        } 
       } 
      } 

      if (ii==-1 && myScore >0) { 
       structArray[0].name = nametextbox.text; 
       structArray[0].highScore = myScore; 
       ii=0; 
      } 
      for (i=0; i<=ii; i++) { 
       [userPreferences setObject:structArray[i].name forKey:[NSString stringWithFormat:@"highScoreNameEntry%d",i]]; 
       [userPreferences setInteger:structArray[i].highScore forKey:[NSString stringWithFormat:@"highScoreEntry%d",i]]; 
      } 
     } 
    } 

} 
+0

喜卡尔,可我知道ü编辑哪个部分。 – lol 2010-12-01 18:25:28

+1

他修复了你破碎的代码格式 – vikingosegundo 2010-12-01 18:28:08

回答

3

任何保存到用户默认值必须是一个standard PLIST-compliant class(NSArray中,的NSDictionary,的NSNumber,NSString的,的NSDate,NSData的,NSValue)。如果它不是其中之一,它必须作为一个NSData实例存档(并且因此是NSCoding兼容的),或者如果它兼容,则打包到NSValue中。

也就是说,你让自己变得比你需要的要困难得多。为什么没有一个包含名称(NSString)和分数(NSNumber)的字典数组(高分描述符容器)的字典(每个描述符都是高分的描述符)?每个字典都描述给定名称的高分。然后您可以使用-setObject:yourArray forKey:@"HighScores"来存储整个事物。

由于您只能从NSUserDefaults中获得不可变副本,因此您希望在修改内容时要求获得高分数组的-mutableCopy(不要忘记释放它)。要么替换分数,要么删除/添加。

使用这种方法,您不必诉诸于(对不起)可怕的“字符串#”方法与固定数量的分数。你的数组只包含现有的高分。没有任何浪费和所有完全PLIST'able 100%标准的可可类没有额外的工作。这还使您可以使用排序描述符(按照用于在字典中存储分数的键进行排序)对数组进行排序。

一些基本代码:

/* Build a fake high scores array */ 

NSMutableArray * highScores = [NSMutableArray array]; 

// Bob's score 
NSDictionary * bob = [NSDictionary dictionaryWithObjectsAndKeys: 
     @"Bob", @"name", 
     [NSNumber numberWithInt:8234323], @"score", 
     nil]; 
[highScores addObject:bob]; 

// Jane's score 
NSDictionary * jane = [NSDictionary dictionaryWithObjectsAndKeys: 
     @"Jane", @"name", 
     [NSNumber numberWithInt:92346345], @"score", 
     nil]; 
[highScores addObject:jane]; 

// Donald's score 
NSDictionary * donald = [NSDictionary dictionaryWithObjectsAndKeys: 
     @"Donald", @"name", 
     [NSNumber numberWithInt:5272348], @"score", 
     nil]; 
[highScores addObject:donald]; 

// Sort by high score 
NSSortDescriptor * sort = [[NSSortDescriptor alloc] initWithKey:@"score" 
                 ascending:NO]; 
[highScores sortUsingDescriptors:[NSArray arrayWithObject:sort]]; 
[sort release]; 

// Store in user defaults 
[[NSUserDefaults standardUserDefaults] setObject:highScores 
              forKey:@"HighScores"]; 
0

不要忘记使用:

[userPreferences synchronize];