开发者

Is there a better way to do this? Want to use a loop, but it breaks [closed]

开发者 https://www.devze.com 2023-03-20 23:52 出处:网络
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time,or an extraordinarily narrow situation that is not generally applic
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 11 years ago.

I have a the following chunk of code that's adding up to six Player objects to six Seat UIView objects. I'm adding them using data stored in an NSMuteableDictionary.

Here's how it looks at the moment:

MoneyCentralAppDelegate *delegate = (MoneyCentralAppDelegate *) [[UIApplication sharedApplication] delegate];

//add player1
NSMutableDictionary *player1Dictionary = [[delegate appDataDictionary] valueForKey:@"Player1"];
Player *player1 = [[Player alloc] initWithFrame:CGRectMake(0,0,0,0)];
player1.playerName = [player1Dictionary valueForKey@"PlayerName"];
player1.avatar = [UIImage imageNamed:[player1Dictionary valueForKey:@"Avatar"]];
[seat1 setAlpha:1];
[seat1 addSubView:player1];

//add player2
NSMutableDictionary *player2Dictionary = [[delegate appDataDictionary] valueForKey:@"Player2"];
if ([player2Dictionary valueForKey:@"Pl开发者_如何学运维ayerName"] != @"Enter your name") {
 Player *player2 = [[Player alloc] initWithFrame:CGRectMake(0,0,0,0)];
 player2.playerName = [player2Dictionary valueForKey@"PlayerName"];
 player2.avatar = [UIImage imageNamed:[player2Dictionary valueForKey:@"Avatar"]];
 [seat2 setAlpha:1];
 [seat2 addSubView:player2];
}

//And so on for another 4 more players...

Isn't there a way to streamline this code by using a loop? I've tried the following but it doesn't work:

for (int i=1; i=6, i++) {
 [self addPlayerToBoard:i];
}

- (void) addPlayerToBoard:(int)playerNumber {

MoneyCentralAppDelegate *delegate = (MoneyCentralAppDelegate *) [[UIApplication sharedApplication] delegate];
NSMutableDictionary *playerDictionary;
NSString *thePlayer;
Seat *seat;

switch (playerNumber) {
 case 1:
  thePlayer = @"Player1";
  seat = seat1;
  break;
 case 2:
  thePlayer = @"Player2";
  seat = seat2:
  break;
 case 3:
  //and so on to 6
}

playerDictionary = [[delegate appDataDictionary] valueForKey:thePlayer];
if ([playerDictionary valueForKey:@"PlayerName"] != @"Enter your name") {
 Player *newPlayer = [[Player alloc] initWithFrame:CGRectMake(0,0,0,0)];
 newPlayer.playerName = [playerDictionary valueForKey:@"PlayerName"];
 newPlayer.avatar = [UIImage imageName:[playerDictionary valueForKey:@"Avatar"]];
 [seat setAlpha:1];
 [seat addSubView:newPlayer];
}
}

As far as I can see this code is good. It compiles with no errors or warnings but when I try to start a new game the app just freezes when it attempts to run that code.

Also just as a side question, the if statement where it checks if the value of PlayerName != @"Enter your name" apparently evaluates to true not matter what because I end up with six players every time. Is that the wrong way to check the value of a string in an nsmutabledictionary?

I know this is a lot to look at but I'd really appreciate any help or suggestions here.

Thanks a ton.


The problem is the way you created your for loop. The proper syntax is: for(initialize; test; update). To iterate over the variable i from 1 to 6, you should use:

for(int i = 1; i <= 6; i++) {
    [self addPlayerToBoard:i];
}

When you use the != or == operators on an object, you are comparing the object's pointer. This means that two objects will only be the same if they are exactly the same object, not just equivalent. To determine if two objects are the same, you should use [object1 isEqualTo:object2]. If you know an object will be an NSString, you should use [string1 isEqualToString:string2] instead, as it will be faster.

if (![[playerDictionary valueForKey:@"PlayerName"] isEqualToString:@"Enter your name"]) {
    ...
}
0

精彩评论

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