开发者

Recursively creating an object and copying specific objects from an array in said object

So, I'm struggling a bit with my programming project.

I have a object that stores player information, name, wins, losses.

I proceed to use that object in another object (a bracket) that sets the values of the player information, after each loop it copies the player object to a NSMutable in the bracket class.

Within the bracket class I have a method to print out the information, which was easy enough to figure out.

Now, to generate what will happen for the next bracket, I set up a method that will return a bracket when it's finished copying who won or lost.

I've done this all within the same bracket method so I could access the data as easily as possible. How do I simply just make a copy of the player object and add it to the new bracket?

I'm finding that I'm just getting garbage values or nothing at all when I try to print out the Bracket I've just generated. Here's my function that I'm having trouble with.

-(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];//To开发者_JAVA百科o 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);
}

So, I thought that would just work. It compiles just fine (I get a few warnings, but it's about the integers and such that I use for logic mostly).

Thank You!


My comments:

The definition of selection is incorrect. You have defined it as a pointer to an int, but you are using it as if it were an int everywhere in your code. Note that selection++ increments selection by sizeof(int) not 1.

You shouldn't really be using -new to initialise objects but -alloc and then -init. (This is the modern convention).

The most likely cause of your problem is that playerarray is not initialised.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜