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

  • SEARCH
  • Home
  • 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 6796399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:27:46+00:00 2026-05-26T18:27:46+00:00

I’m observing some strange behavior with a local variable within a nested for loop

  • 0

I’m observing some strange behavior with a local variable within a nested for loop in objective-c.

The code below loops through an NSMutableArray called visitors that can range in size from 1 to 20.

I store a local copy of the object at position i and then I work out it’s bounding box.

I then loop through the array again within the first loop, picking out a second object from the list and working out it’s bounding box.

The collision check is almost never reached when the objects on screen over lap.

When I step through the code using the debugger I observe within the second for loop that once the variable visitor2 is set it never changes from that point on. It continues to point at the same object until the function call is complete.

All objects within the visitors array are definitely totally unique objects as in the same object isn’t added to the array twice I made sure to check for this.

I have no idea what could be causing this, I have never seen anything like it. Any help would be much appreciated.

Here is my code:

for (int i = 0; i < [visitors count]; i++) 
{

    // Obtain visitor from the list
    Visitor* visitor1 = [visitors objectAtIndex: i];

    // Calculate it's bounding box
    CGRect visitor1rect = CGRectMake(visitor1.position.x - visitor1.size.x/2, 
                              visitor1.position.y - visitor1.size.y/2, 
                              visitor1.size.x, visitor1.size.y);

    // Loop through all other visitors
    for (int j = 0; j < [visitors count]; j++) 
    {
        // Don't check for a collision with self
        if (i != j) 
        {
            // Obtain a visitor from the list
            Visitor* visitor2 = [visitors objectAtIndex: j];

            // Calculate it's bounding box
            CGRect visitor2rect = CGRectMake(visitor2.position.x - visitor2.size.x/2, 
                                      visitor2.position.y - visitor2.size.y/2, 
                                      visitor2.size.x, visitor2.size.y);

            // Chcek of the two bounding boxes intersect
            if ( CGRectContainsRect(visitor1rect, visitor2rect) ) {

                // Do stuff

            }

        }
    }
}
  • 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-26T18:27:47+00:00Added an answer on May 26, 2026 at 6:27 pm

    Use CGRectIntersectsRect for your intersection test instead of “contains”, which means something different. If that fixes your problem, I’d guess that the debugger is just lying to you about the thing visitor2 points to– the code looks fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
Does anyone know how can I replace this 2 symbol below from the string
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.