-2

I have some integer values inside NSMutableArray. I have added a UITextField and a UIButton. If a number is entered inside textfield and clicking on the button does a comparison. If number entered matches, I need to show NSLog. But it's not working.

code:

arr = [[NSMutableArray alloc]init];
[arr addObject:[NSNumber numberWithInteger:1]];

Button click:

-(void)click:(id)sender{
    if (text.text == [arr objectAtIndex:0]){
        NSLog(@"values matched");
    }
}
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
user2674668
  • 117
  • 2
  • 13
  • 2 problems: You cannot compare .text property NSString with the == operator. You. Need the `isEqualToString:` to compare strings, also the array item is not an NSString, but NSNumber. – Rob van der Veer Aug 30 '13 at 08:32

2 Answers2

3

Try this

-(void)click:(id)sender{

    NSString *str = [NSString alloc]initWithFormat:@"%d",[arr objectAtIndex:0]];
        if([text.text isEqualToString: str]){

            NSLog(@"values matched");


        }
    }
Bug
  • 2,576
  • 2
  • 21
  • 36
2

I am assuming the array contains NSNumber objects; and if so convert the textfield content to an NSNumber object and use [NSArray indexOfObject] to find it in the array:

- (void)click:(id)sender{
    NSNumber *num = [NSNumber numberWithInt:[text.text intValue]];
    NSUInteger index = [arr indexOfObject:num];
    if (index != NSNotFound) {
        NSLog(@"values matched");
    }
}
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • @middaparka It's still not clear, however if the OP wants to match the text field against *any* value in the array or just the first object... – trojanfoe Aug 30 '13 at 07:56
  • I think your "check all objects in the array" approach exactly matches what the OP describes in the text. (I guess the supplied code is but a step in that direction and that iteration would have been added next.) – John Parker Aug 30 '13 at 07:58
  • @middaparka I guess we'll find out later; the OP has asked a classic "fire and forget" question... – trojanfoe Aug 30 '13 at 08:00
  • Tis the way these days it (sadly) seems. (Greetings from down the road in Bournemouth incidentally.) :-) – John Parker Aug 30 '13 at 08:03
  • @middaparka Cheers - even closer at the moment - at work in (gloomy) Romsey. – trojanfoe Aug 30 '13 at 08:04
  • @trojanfoe: It's working. How to use this for loop. N number of values. Now im checking with one value only – user2674668 Aug 30 '13 at 08:47
  • @user2674668 I don't quite understand. What values do you want to check in a loop? – trojanfoe Aug 30 '13 at 08:53
  • arr Array values. If i enter 0 then i click check button, if 0 is match i need to return result. Now im checking only one value. How to use more values check. – user2674668 Aug 30 '13 at 08:57
  • @user2674668 The code I provided will check every value in the array already. – trojanfoe Aug 30 '13 at 08:58
  • Then i need to write [arr addObject:[NSNumber numberWithInteger:1]]; [arr addObject:[NSNumber numberWithInteger:0]]; [arr addObject:[NSNumber numberWithInteger:2]]; for every values – user2674668 Aug 30 '13 at 09:07
  • @user2674668 Sorry it's still not clear what you mean. Start a new question and someone will solve your new issue pretty quickly. – trojanfoe Aug 30 '13 at 09:09
  • If i didn't enter anything in UITextfield, then i click the check button it's showing values matched. Your answer is wrong. @kapil Maheshwari answer correct. – user2674668 Aug 30 '13 at 09:18
  • 2
    @user2674668 Well it looked to me that you wanted to check every value in the array. If you want to guard against an empty selection then simply check the length of the textfield content: `if ([text.text length] == 0) { /* don't test array */ };`. I would recommend asking the question more clearly so that I, and others, don't waste their time attempting to solve your issues. – trojanfoe Aug 30 '13 at 09:29