views:

240

answers:

2

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!

+2  A: 

An '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:

  1. Create an NSEnumerator that will enable you to step through the array
  2. Loop through that enumerator
  3. 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.

h4xxr
NSEnumerator isn't necessary (for this simple case) since Leopard. The new way to loop on a collection object is called fast enumeration: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocFastEnumeration.html
Peter Hosey
I see. Don't I need an IBOutlet to the NSTextField? Also is there a way to make it check more than one String?
Joshua
@Peter, I know, I did mention fast enumeration in my post. Just felt this was simplest approach for newcomer. @Joshua, yes if you are checking an NSTextField you'll need an IBOutlet to access its contents in code. I'll edit and add a way to check more than one field...
h4xxr
Thanks! But thats not what I meant I want one text field but to check the text in that one text field to more than one string. ;)
Joshua
Aha! Ok, well essentially you'd need multiple enumerators. I'll stick in another edit...
h4xxr
I see. Still one question, where do I put the text which I want to see if it matches the text in the Text Field?
Joshua
Also I get five errors with the code you gave me. http://snapplr.com/85x7
Joshua
Fixed the errors, you forgot a curly bracket. ;)
Joshua
Doh! First line, well spotted. As to where you put the text - is that not in the original question? You were saying you had one (or more) NSArrays filled with strings. That's where I'm assuming the text to compare is
h4xxr
hmmm, I've probably earned a +1 here haven't I Joshua?...
h4xxr
No I don't have an NSArray yet. Mind helping me there? And Yes definitely deserves a +1 :). And I'll mark it as the correct answer if you help me with the NSArray. ;)
Joshua
:) ok, so how will we need to fill this NSArray? Do you know the text that you'd want to put it in now? Or will you only know that at run-time (based on data in some way)?
h4xxr
Put it in now. Thanks. :)
Joshua
Yeh, I know what text I wan't to put it now.
Joshua
Ok, have made another edit :)
h4xxr
Ok. Thanks. Here's a picture of what code I've got now. http://snapplr.com/y1nn .Just one problem and that's the error. Why am I getting it?
Joshua
Sorry, was thinking iPhone (UITextField) whereas you want Mac (NSTextField). Swap .text for .stringValue
h4xxr
Ok. It nearly works. Here's a picture to explain http://snapplr.com/92aa . As you can see if the String in the text field matches a string in the array it will return no. What I have done is made a window appear when it returns no. But this doesn't happen. Another pic. http://snapplr.com/7356 . If I put return no for the while statement it will show the window, but all the time. It's as if the return no from the if statement is not getting through.
Joshua
Or it's not checking the two strings correctly.
Joshua
That's a big array you've got - shouldn't each 5-letter code (eg GAWQE) be a separate element in the array? Then that would look like this: @"GAWQE",@"FDPMS",@"RWMVL" etc etc... If that doesn't sort it I suggest you email me ([email protected]) so we don't over-use the comments system!
h4xxr
No it has to be one whole string. I'll email you. :)
Joshua
+3  A: 

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.

Peter Hosey
+1 Fair, for the precise example (comparing on a string) there is no enumeration needed. However as you say, as soon as it extends beyond that he'll need to loop on the array
h4xxr