Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 更快的if语句_Iphone_Objective C_Ios_Xcode_If Statement - Fatal编程技术网

Iphone 更快的if语句

Iphone 更快的if语句,iphone,objective-c,ios,xcode,if-statement,Iphone,Objective C,Ios,Xcode,If Statement,我正在尝试创建一个iOS应用程序,其中包含一个主if语句,用于检查文本框是否包含某些文本。我想检查某个单词的多个实例,但我不想执行多个if语句,因为这会大大降低应用程序的性能 如何检查某个单词是否与NSArray中某个单词的匹配,如果匹配,则在另一个文本框中返回与该单词相关联的NSString?您可以创建一个NSDictionary,将要检查的单词映射到输入该单词时要返回的NSString: NSDictionary *words = [[NSDictionary alloc] initWith

我正在尝试创建一个iOS应用程序,其中包含一个主if语句,用于检查文本框是否包含某些文本。我想检查某个单词的多个实例,但我不想执行多个if语句,因为这会大大降低应用程序的性能

如何检查某个单词是否与NSArray中某个单词的匹配,如果匹配,则在另一个文本框中返回与该单词相关联的NSString?

您可以创建一个NSDictionary,将要检查的单词映射到输入该单词时要返回的NSString:

NSDictionary *words = [[NSDictionary alloc] initWithObjectsAndKeys:
    @"word1", @"result1",
    @"word2", @"result2",
    /*etc*/
    nil];

NSString *wordFromTextBox = ...;
NSString *result = [words objectForKey:keyword];
if (result != nil)
{
    // use result
}
else
{
    // word wasn't something we expected
}

// Some time later
[words release];
编辑:要在列表中进行迭代,以便测试文本框中是否包含关键字,可以执行以下操作:假设Word和wordFromTextBox的设置与之前相同:

NSEnumerator *enumerator = [words keyEnumerator];
while ((NSString *keyword = [enumerator nextObject]) != nil)
{
    if ([wordFromTextBox rangeOfString:keyword].location != NSNotFound)
    {
        NSLog(@"The text box contains keyword '%@'", keyword);
        NSString *result = [words objectForKey:wordFromTextBox];
        // Use the result somehow
        break;
    }
}
您可以创建一个NSDictionary,将要检查的单词映射到输入该单词时要返回的NSString:

NSDictionary *words = [[NSDictionary alloc] initWithObjectsAndKeys:
    @"word1", @"result1",
    @"word2", @"result2",
    /*etc*/
    nil];

NSString *wordFromTextBox = ...;
NSString *result = [words objectForKey:keyword];
if (result != nil)
{
    // use result
}
else
{
    // word wasn't something we expected
}

// Some time later
[words release];
编辑:要在列表中进行迭代,以便测试文本框中是否包含关键字,可以执行以下操作:假设Word和wordFromTextBox的设置与之前相同:

NSEnumerator *enumerator = [words keyEnumerator];
while ((NSString *keyword = [enumerator nextObject]) != nil)
{
    if ([wordFromTextBox rangeOfString:keyword].location != NSNotFound)
    {
        NSLog(@"The text box contains keyword '%@'", keyword);
        NSString *result = [words objectForKey:wordFromTextBox];
        // Use the result somehow
        break;
    }
}

我会使用NSMutableDictionary/NSDictionary。字典键将是您要查找的单词。这些值将是您希望返回的单词。一旦设置好字典,只需调用[dict valueForKey:textbox.text]。如果条目不在其中,dict将返回nil,因此您可以测试该条件。总而言之,我认为这可能是最快的方法。

我会使用NSMutableDictionary/NSDictionary。字典键将是您要查找的单词。这些值将是您希望返回的单词。一旦设置好字典,只需调用[dict valueForKey:textbox.text]。如果条目不在其中,dict将返回nil,因此您可以测试该条件。总而言之,我认为这可能是最快的方法。

对于阵列, 您可以从以下代码中获得想法:

NSArray* myArray = [[NSArray alloc] initWithObjects:@"one",@"two",@"three",@"four", nil];
NSString* stringToSearch = @"four";
NSUInteger indexPositionInArray = [myArray indexOfObject:stringToSearch];
NSLog(@"position in array: %i", indexPositionInArray);
if (indexPositionInArray !=  NSNotFound) {
    NSLog(@"String found in array: %@", [myArray objectAtIndex:indexPositionInArray]);
}

NSString* stringToSearch2 = @"fourrrr";
NSUInteger indexPositionInArray2 = [myArray indexOfObject:stringToSearch2];
NSLog(@"position in array: %i", indexPositionInArray2);
if (indexPositionInArray2 !=  NSNotFound) {
    NSLog(@"String found in array: %@", [myArray objectAtIndex:indexPositionInArray]);
}
对于数组, 您可以从以下代码中获得想法:

NSArray* myArray = [[NSArray alloc] initWithObjects:@"one",@"two",@"three",@"four", nil];
NSString* stringToSearch = @"four";
NSUInteger indexPositionInArray = [myArray indexOfObject:stringToSearch];
NSLog(@"position in array: %i", indexPositionInArray);
if (indexPositionInArray !=  NSNotFound) {
    NSLog(@"String found in array: %@", [myArray objectAtIndex:indexPositionInArray]);
}

NSString* stringToSearch2 = @"fourrrr";
NSUInteger indexPositionInArray2 = [myArray indexOfObject:stringToSearch2];
NSLog(@"position in array: %i", indexPositionInArray2);
if (indexPositionInArray2 !=  NSNotFound) {
    NSLog(@"String found in array: %@", [myArray objectAtIndex:indexPositionInArray]);
}


以你认为慢的方式编写,如果慢,就发布代码。它可能不会像你想象的那么慢-你在说多少个单词?你测试过多个if语句了吗,看看它们是否真的慢?因为这似乎不太可能……会有几百个。这就像一个问答服务。在文本框中提问,按go键,它会检查文本框中是否存在单词,以及是否存在单词,它返回一个值…尝试使用字典/哈希表。我从您的评论中感觉到,您应该有一个数据库,可能是SQLite3,并使用SELECT查询来检索包含文本框中部分或全部输入文本的文本。以您认为慢的方式编写,如果慢,则发布代码。它可能不会像你想象的那么慢-你在说多少个单词?你测试过多个if语句了吗,看看它们是否真的慢?因为这似乎不太可能……会有几百个。这就像一个问答服务。在文本框中提问,按go键,它会检查文本框中是否存在单词,以及是否存在单词,它返回一个值…尝试使用字典/哈希表。我从您的评论中感觉到,您应该有一个数据库,可能是SQLite3,并使用SELECT查询来检索文本框中包含部分或全部输入文本的文本。@objc-consive我在第一次编辑时漏掉了alloc!我现在已经修好了。我如何用它来检查搜索文本框中是否有单词1,而周围可能有其他单词。i、 e.它仍然会检测到这一点;这是word1,尽管它有其他words@objc-它要求您在NSDictionary中迭代每个关键字,并测试关键字是否出现在搜索文本中。那不会那么快。这确实需要一个新的问题。例如,大约需要多长时间来检查100个键?你能告诉我这里的情况吗@objc-consive 100个值不会花很长时间。@objc-consive我在第一次编辑时错过了alloc!我现在已经修好了。我如何用它来检查搜索文本框中是否有单词1,而周围可能有其他单词。i、 e.它仍然会检测到这一点;这是word1,尽管它有其他words@objc-它要求您在NSDictionary中迭代每个关键字,并测试关键字是否出现在搜索文本中。那不会那么快。这确实需要一个新的问题。例如,大约需要多长时间来检查100个键?你能告诉我这里的情况吗@objc痴迷的100个价值观不会花很长时间。