Try this
-(void)click:(id)sender{
NSString *str = [NSString alloc]initWithFormat:@"%d",[arr objectAtIndex:0]];
if([text.text isEqualToString: str]){
NSLog(@"values matched");
}
}
Question
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");
}
}
La solution
Try this
-(void)click:(id)sender{
NSString *str = [NSString alloc]initWithFormat:@"%d",[arr objectAtIndex:0]];
if([text.text isEqualToString: str]){
NSLog(@"values matched");
}
}
Autres conseils
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");
}
}