【问题标题】:can i sort stored score in nsuserdefaults?我可以在 nsuserdefaults 中对存储的分数进行排序吗?
【发布时间】:2012-09-04 06:40:50
【问题描述】:

我正在开发游戏应用程序,在该应用程序中我将名称、分数存储在 nsuserdefaults ....但是 根据我的要求,我想根据分数存储名称和分数(从高到低)......有没有在nsuserdefaults中排序分数的解决方案......我想在顶部显示我的高分

感谢广告

-(void)btnSaveScore { 如果(!dictWinData) dictWinData = [[NSMutableDictionary alloc] init];

    array = [[NSMutableArray alloc] init];
    array = [[[NSUserDefaults standardUserDefaults] valueForKey:@"ScoreName"] mutableCopy];
    if([array count] == 0)
    {
        array = [[NSMutableArray alloc] init];
    }

NSString *strName = [NSString stringWithFormat:@"%@",strNameOFPlayer];
NSString *strScore = [NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%d",iTap]];

if([strNameOFPlayer length]==7)
    [array addObject:[NSString stringWithFormat:@"%@                   %@",strName,strScore]];
else if ([strNameOFPlayer length] == 6)
    [array addObject:[NSString stringWithFormat:@"%@                     %@",strName,strScore]];
else if ([strNameOFPlayer length] == 5)
    [array addObject:[NSString stringWithFormat:@"%@                       %@",strName,strScore]];
else if ([strNameOFPlayer length] == 4)
    [array addObject:[NSString stringWithFormat:@"%@                         %@",strName,strScore]];
else if ([strNameOFPlayer length] == 3)
    [array addObject:[NSString stringWithFormat:@"%@                           %@",strName,strScore]];
else if ([strNameOFPlayer length] == 2)
    [array addObject:[NSString stringWithFormat:@"%@                             %@",strName,strScore]];
else if ([strNameOFPlayer length] == 1)
    [array addObject:[NSString stringWithFormat:@"%@                              %@",strName,strScore]];

   NSUserDefaults *dfltsData = [NSUserDefaults standardUserDefaults];
   [dfltsData setObject:array forKey:@"ScoreName"];
   [dfltsData synchronize];
   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                message:@"Score is saved."
                                               delegate:self
                                      cancelButtonTitle:@"Ok"
                                      otherButtonTitles:nil, nil];
          [alert show];
          [alert release];

请帮帮我

【问题讨论】:

  • pastebin.com/mJGJq3sa 请检查我的新代码......现在 NSMutableArray *sortedArray = [self sortByfloatvalue:scoreCardArray array:playerarray]; arrScores = [sortedArray objectAtIndex:0]; arrPlayers = [sortedArray objectAtIndex:1];这些行
  • 请检查链接pastebin.com/mJGJq3sa我的新代码王子今天是我的项目截止日期的最后一天请plzzz???????在我的代码中引用我的错误????希望你理解我的问题?
  • NSMutableArray *sortedArr = [NSMutableArray alloc]init;替换为 NSMutableArray *sortedArr = [NSMutableArray 数组];在 sortByfloatvalue 中。 NSLog sortedArray

标签: iphone ios nsuserdefaults


【解决方案1】:

像这样从 NSUserDefault 获取数组:

NSMutableArray *arrScores = [[NSUserDefaults standardUserDefaults] ObjectforKey:@"ScoreName"];

现在排序 arrScores 应该只包含 strScore 值,只能使用以下方法:

-(NSMutableArray *)sortByfloatvalue:(NSMutableArray *)array
{ 

  for(int i=0;i<[array count];i++)
  {
    for(int j=i+1;j<[array count];j++)
    {
        if([[array objectAtIndex:i] floatValue] < [[array objectAtIndex:j] floatValue])
        {
            [array exchangeObjectAtIndex:i withObjectAtIndex:j];
        }
    }
  }
  return array;
}

【讨论】:

  • 在从高到低的排序数组中,它将显示前十名,如下所示: if([soretedArray count]>=10){for(int i=0; i
猜你喜欢
  • 2013-05-31
  • 1970-01-01
  • 2018-10-31
  • 2018-06-19
  • 1970-01-01
  • 1970-01-01
  • 2010-12-04
  • 1970-01-01
  • 2018-07-08
相关资源
最近更新 更多