1

I have this while loop in my code. The loop seems to work fine since I printed my i++ in the console. But for some reason it only checks my if statement the first time around. I can only add one title into the NSMutableArray called sectionZeroTitleArray. I have many arrays in this loop so it might get confusing. I will try my best to explain.

Here is what I am trying to do: Loop through the length of an array(topicArray). If the array's(topicArray)is the same as this other array's(anotherArray) first object then add an object that has the same index(titleArray) as topicArray to a new MutableArray(sectionZeroTitleArray).

I'm sure I did something stupid, maybe someone that hasn't stared at this all day can fix me up? Please and thank you.

while (i < (topicArray.count)) {
  if ([topicArray objectAtIndex:i] == [anotherArray objectAtIndex:0]) {
   [sectionZeroTitleArray addObject:[titleArray objectAtIndex:i]];
  }
  NSLog(@"sectionZeroLoopCount: %d", i);
  i++;
 }
5
  • Add the following before the if statement: NSLog(@"topicArray value:%@, anotherArray value:%@", [topicArray objectAtIndex:1], [anotherArray objectAtIndex:0]); Commented Sep 28, 2010 at 23:00
  • Does your topicArray contain the exact same object twice? If not, it's quite natural, that it goes only once into the if-brackets. Sure, you don't mean "objectAtIndex:i"? Commented Sep 28, 2010 at 23:00
  • 1
    Also, what are the types of the objects stored in the arrays? Often, you want to use isEqual: or isEqualToString: rather than ==. Commented Sep 28, 2010 at 23:01
  • 2
    You have to define what you mean by "is the same as" means. Right now you have written == which means that the pointers point to the same object (the pointers have the same value). Commented Sep 28, 2010 at 23:07
  • 1
    DUDE, you guys are sick, lol I posted like 5 mins ago and everyone already have the answer. I love this community and you guys are all awesome. If I could, I would give you all check marks. Commented Sep 28, 2010 at 23:09

2 Answers 2

5

You are checking for pointer equality when you use ==. Are you sure you want to do this? What is the type that you're expecting? If it's a NSString, use isEqualToString:, otherwise use NSObject's isEqual: method:

If the expected type is an NSString:

if([[topicArray objectAtIndex:i] isEqualToString:[anotherArray objectAtIndex:0]]) {
   //...
}

Otherwise, you should probably do this:

if([[topicArray objectAtIndex:i] isEqual:[anotherArray objectAtIndex:0]]) {
   //...
}
Sign up to request clarification or add additional context in comments.

Comments

1

Yeah, you're comparing the pointers and not the values. Look at the documentation of NSString, particular the isEqualToString: method for comparing strings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.