I am trying to make my app check the text in a text box and see if it matches any of the strings in an NSArray. I'm hoping you could point me in the right direction or give me some code to do it. I am pretty sure that I would need to use an if
statement.
Thanks!
views:
240answers:
2An 'if' statement is a pretty good assumption! - one of the basic tenets (if not the basic tenet) of all programming languages...
There are 101 ways you could do this, so you'll probably get a bunch of replies.
What you want to do in essence is this:
- Create an NSEnumerator that will enable you to step through the array
- Loop through that enumerator
- Check the contents of the array each time
Note that there are all sorts of other approaches too, including using something called "fast enumeration", but I'll leave you to read up on these yourself.
Ok, so time for some example code:
NSEnumerator *enumerator;
enumerator = [myArray objectEnumerator]; // myArray is what we're checking
NSString *stringContents;
while (stringContents = [enumerator nextObject]) { // Cycles through array
if ([stringContents isEqualToString:@"SOMETHING"]) {
// We have a match! Call a method or something...
}
}
Like I said, many other ways of doing this too, but hopefully this will get you started!
Edit: If you want to examine multiple items, eg textField1, textField2 and textField3, your code could look like this (|| is the symbol for a logical "OR"):
if ([stringContents isEqualToString:textField1.text] ||
[stringContents isEqualToString:textField2.text] ||
[stringContents isEqualToString:textField3.text]) {
This would evaluate to true if any of the three textFields matched your array content. If you wanted to only evaluate to true if ALL of your statements matched, swap || for &&.
Edit 2: If you want to compare to multiple NSArrays, it makes sense to put the array enumeration and comparison into a separate method which takes an array and a string, and perhaps returns a BOOL dependent upon whether or not a match was found:
New method:
-(BOOL)checkArray:(NSArray *)myArray forString:(NSString *)myString {
NSEnumerator *enumerator;
enumerator = [myArray objectEnumerator];
NSString *stringContents;
while (stringContents = [enumerator nextObject]) { // Cycles through array
if ([stringContents isEqualToString:myString]) {
return YES;
}
}
return NO;
}
And you can then call this for multiple arrays:
BOOL result1 = [self checkArray:array1 forString:textField.text];
BOOL result2 = [self checkArray:array2 forString:textField.text];
// Etc...
Hope that all helps!...
Edit 3: If you need to fill the arrays with search terms, there is a nice convenience method for doing so:
NSArray *myArray = [NSArray arrayWithObjects:@"Item 1",@"Item 2", nil];
Note a couple of important things: each item is preceded by an @ symbol if it's text in quotes, and the last item must always be the word nil. You can put as many items (pretty much) as you want.
Send a containsObject:
message to the array, passing the string you want to check for. That method will use isEqual:
messages to compare your string to every object in the array. The message will return a BOOL
, which you can test in your if
statement.
If you want a more sophisticated definition of “matching” than looking for an equal string, you'll need to loop on the array yourself and perform your desired test on each object in the array one at a time.
Also, be aware that isEqual:
/isEqualToString:
comparisons are case-sensitive: @"foo"
is not equal to @"Foo"
. If you want case-insensitive or locale-aware comparisons, you'll need to loop on the array yourself and use one of NSString's other comparison methods.