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++ は、選択を 1 ではなく sizeof(int) だけインクリメントすることに注意してください。

-new を使用してオブジェクトを初期化するのではなく、-alloc を使用してから -init を使用する必要があります。(これは現代の慣習です)。

問題の最も可能性の高い原因は、playerarray が初期化されていないことです。

于 2010-11-04T10:04:32.947 に答える