使代码检查以查看文本框中的文本是否匹配NSArray中的任何字符串 [英] Making the Code check to see if the Text in a Text box matches any of the Strings in an NSArray

查看:141
本文介绍了使代码检查以查看文本框中的文本是否匹配NSArray中的任何字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的应用程序检查文本框中的文本,看看它是否匹配NSArray中的任何字符串。我希望你能指向我正确的方向或给我一些代码来做。我很肯定,我需要使用 if 语句。
谢谢!

解决方案

'if'语句是一个很好的假设! - 所有编程语言的基本原则之一(如果不是基本原则)...



有101种方法可以做到这一点,所以你可能会得到





  • 每次检查数组的内容
  • 创建一个NSEnumerator, li>

    请注意,还有各种其他方法,包括使用称为快速枚举的东西,但我会让您阅读



    好,所以时间为一些示例代码:

      NSEnumerator * enumerator; 
    enumerator = [myArray objectEnumerator]; // myArray是我们要检查的内容
    NSString * stringContents;

    while(stringContents = [enumerator nextObject]){//循环遍历数组
    if([stringContents isEqualToString:@SOMETHING]){
    //我们有一个匹配!调用一个方法或某事...
    }
    }



    编辑:如果您想检查多个项目,例如: textField1,textField2和textField3,代码看起来像这样(||是逻辑OR的符号):

      if([stringContents isEqualToString:textField1.text] || 
    [stringContents isEqualToString:textField2.text] ||
    [stringContents isEqualToString:textField3.text]){

    如果三个textField中的任何一个匹配您的数组内容,这将评估为true。如果你只想评估为真如果所有的语句匹配,交换|| for&&&&&&&&&&&&&&< / p>

    Edit 2:如果要比较多个NSArrays,到一个单独的方法,它接受一个数组和一个字符串,并可能返回一个BOOL,取决于是否找到匹配:



    新方法:

       - (BOOL)checkArray:(NSArray *)myArray forString:(NSString *)myString {
    NSEnumerator * enumerator;
    enumerator = [myArray objectEnumerator];
    NSString * stringContents;

    while(stringContents = [enumerator nextObject]){//循环遍历数组
    if([stringContents isEqualToString:myString]){
    return YES;
    }
    }
    return NO;
    }

    然后你可以为多个数组调用:

      BOOL result1 = [self checkArray:array1 forString:textField.text]; 
    BOOL result2 = [self checkArray:array2 forString:textField.text];
    // Etc ...

    希望所有的帮助! >

    编辑3:如果您需要使用搜索字词填充数组,则可以使用一个非常方便的方法:

      NSArray * myArray = [NSArray arrayWithObjects:@Item 1,@Item 2,nil]; 

    注意几个重要的事情:每个项目前面都有一个@符号,且最后一个项目必须始终为字 nil 。您可以根据需要放置很多项目(很多)。


    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!

    解决方案

    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.

    这篇关于使代码检查以查看文本框中的文本是否匹配NSArray中的任何字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆