Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6147317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:05:22+00:00 2026-05-23T19:05:22+00:00

I have a the following chunk of code that’s adding up to six Player

  • 0

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:@"PlayerName"] != @"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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:05:22+00:00Added an answer on May 23, 2026 at 7:05 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say you have the following chunk of code: <div id=container> <someelement>This is any
I am learning jQuery. I have the following chunk of code in an HTML
I came across the following weird chunk of code.Imagine you have the following typedef:
I have the following chunk of a header file BKE_mesh.h: /* Connectivity data */
I have a chunk of code that on page load with populates some of
I have the following chunk of code in my footer.phtml <!-- Customer Modal -->
I have a file that was encrypted using AES. I use the following NSData
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following Code Block Which I tried to optimize in the Optimized section

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.