i write a method that loops through an array of buttons and checks if a string is equal to any of the buttons titles inside the array, but it doesn’t work although the string passed to that method equals some strings inside the array, here’s my code:
-(void)checkDuplicatesInSection:(NSString*)btnLabel
{
for (UIButton* btn in self.test) {
if([btnLabel isEqualToString:btn.titleLabel.text])
{
NSLog(@"Inside check Dublicates--->Title Existed");
} else {
NSLog(@"Inside check Dublicates--->Title Not Existed");
}
}
}
// self.test---> it's an array contains group of buttons
// btnLabel----> it's a string passed to that method
What I don’t understand is why when I run the program, I get both Inside check Dublicates--->Title Existed and "Inside check Dublicates--->Title Not Existed.
The code:
will be executed multiple times because it is in a
forloop. That’s why you get both logs printed when you run your code.To test whether
self.testcontains the stringbtn.titleLabel.textyou should modify your code into:Or you can simply use the method
-containsObject:*:* This will work even if
btn.titleLabel.textis anNSString.