0

所以,我在我的编程项目上有点挣扎。

我有一个存储玩家信息、姓名、胜利、失败的对象。

我继续在另一个对象(括号)中使用该对象,该对象设置播放器信息的值,在每个循环之后,它将播放器对象复制到括号类中的 NSMutable。

在括号类中,我有一个打印信息的方法,这很容易弄清楚。

现在,为了生成下一个括号会发生什么,我设置了一个方法,当它完成复制谁输赢时将返回一个括号。

我已经在同一个括号方法中完成了所有这些操作,因此我可以尽可能轻松地访问数据。我如何简单地复制播放器对象并将其添加到新括号中?

当我尝试打印刚刚生成的括号时,我发现我只是得到垃圾值或什么都没有。这是我遇到问题的功能。

-(Bracket *) NextRound//Configures the next round
{
 int i, y, *selection; //counter and selection
 int comp;
 y = 1;
 i = 0;//so the counter won't get messed up

 Bracket *roundx = [Bracket new]; //creates the bracket that will be sent back with the winners. 

   NSLog(@"Did %@(1) or %@(2) win (1 or 2)?: ",[[playerarray objectAtIndex:i] name], [[playerarray objectAtIndex:y] name]);
  scanf("%d",&selection); 
  comp = selection++;

  if (comp == 1)
  {  
   NSLog(@"Player %d %@ selected to win", i, [[playerarray objectAtIndex:i] name]);


   [[self.playerarray objectAtIndex:i] setNext];//bool value for win or not
   [roundx.playerarray addObject: [self.playerarray objectAtIndex:i]];
   [roundx Print];//Too see if it has anything, usually it does not.

  }
  else
  {
   i++;
   NSLog(@"Player %d %@ selected to win", i, [[playerarray objectAtIndex:i] name]);
   [[self.playerarray objectAtIndex:i] setNext];
   [roundx.playerarray addObject: [self.playerarray objectAtIndex:i]];
   [roundx Print];

  }
 return(roundx);
}

所以,我认为这会奏效。它编译得很好(我收到了一些警告,但它是关于整数的,所以我主要用于逻辑)。

谢谢你!

4

1 回答 1

0

我的评论:

选择的定义不正确。您已将其定义为指向 int 的指针,但您在代码中的任何地方都将其用作 int。请注意,selection++ 按 sizeof(int) 而不是 1 递增选择。

您不应该真正使用-new 来初始化对象,而是使用-alloc 然后-init。(这是现代惯例)。

问题的最可能原因是 playerarray 未初始化。

于 2010-11-04T10:04:32.947 回答