Iphone 如何在数组元素上应用搜索?

Iphone 如何在数组元素上应用搜索?,iphone,objective-c,Iphone,Objective C,我正在iphone应用程序中实现搜索栏,以便在数组元素上应用搜索。 在该数组中,我保存了通过解析从服务器获取的数据。 数据包含以下pdf文件的链接:myArray是一个可变数组,其元素为: http://www.projects-demo.com/iphone/unicurd/recipePagePdf/abc.pdf http://www.projects-demo.com/iphone/unicurd/recipePagePdf/def.pdf http://www.projects-dem

我正在iphone应用程序中实现搜索栏,以便在数组元素上应用搜索。 在该数组中,我保存了通过解析从服务器获取的数据。 数据包含以下pdf文件的链接:myArray是一个可变数组,其元素为:

http://www.projects-demo.com/iphone/unicurd/recipePagePdf/abc.pdf http://www.projects-demo.com/iphone/unicurd/recipePagePdf/def.pdf http://www.projects-demo.com/iphone/unicurd/recipePagePdf/ghi.pdf http://www.projects-demo.com/iphone/unicurd/recipePagePdf/jkl.pdf http://www.projects-demo.com/iphone/unicurd/recipePagePdf/mno.pdf http://www.projects-demo.com/iphone/unicurd/recipePagePdf/pqr.pdf http://www.projects-demo.com/iphone/unicurd/recipePagePdf/stu.pdf http://www.projects-demo.com/iphone/unicurd/recipePagePdf/vwx.pdf http://www.projects-demo.com/iphone/unicurd/recipePagePdf/yz1.pdf http://www.projects-demo.com/iphone/unicurd/recipePagePdf/abc.pdf http://www.projects-demo.com/iphone/unicurd/recipePagePdf/def.pdf http://www.projects-demo.com/iphone/unicurd/recipePagePdf/ghi.pdf http://www.projects-demo.com/iphone/unicurd/recipePagePdf/jkl.pdf http://www.projects-demo.com/iphone/unicurd/recipePagePdf/mno.pdf http://www.projects-demo.com/iphone/unicurd/recipePagePdf/pqr.pdf http://www.projects-demo.com/iphone/unicurd/recipePagePdf/stu.pdf http://www.projects-demo.com/iphone/unicurd/recipePagePdf/vwx.pdf http://www.projects-demo.com/iphone/unicurd/recipePagePdf/yz1.pdf 我正在保存要在searchtext.text中搜索的字符串 现在应该实现什么逻辑来搜索myArray元素中的searchtext.text

谢谢。

您可以这样做:- 让您的数组名folderCheckArray(由您更改)

在搜索栏中,单击SearchButton方法:-

- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar
{
if ([self.folderCheckArray containsObject:theSearchBar.text]) {
    NSLog(@"array contain object"); 
    }
else
{
NSLog(@"array contain object"); 
}
}
如果在数组中搜索之前必须对搜索文本进行任何格式化,则可以编写

if([self.folderCheckArray containsObject:[NSString stringWithFormat:@"%@",theSearchBar.text]]) {
}

创建一个新数组,该数组保存值(如果找到),并将其设置为copyitemcity

搜索代码:-

NSString *searchText = searchBar.text;

for (NSString *str in folderCheckArray)
    {

NSRange titleResultsRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(titleResultsRange.location != NSNotFound)

        {

        if (titleResultsRange.location==0)
        {
            [copyitemcity addObject:str];
        }
        }


}

尝试使用此代码进行搜索。

@Jennifer:非常感谢!!!但通过这种方式,在search bar.text中,我们必须输入与数组元素相同的字符串。但是根据搜索逻辑,search bar.text可以是数组元素的任何子字符串。@archana如果您使用搜索逻辑,那么当您输入字符串时可能需要一个子字符串,如果搜索字符串存在于任何数组值中,您应该得到数组元素的整个值。你需要检查你的搜索代码,你会得到你的解决方案it@Jennifer:它肯定是子字符串,因为数组包含从服务器获取的URL,最终用户不知道/输入要搜索的整个URL。我只想知道字符串作为子字符串与数组元素的比较。如果([self.folderCheckArray containsObject:[NSString stringWithFormat:@“%@”,theSearchBar.text]]){}意味着在数组中搜索文本之前,我可以在搜索文本中使用什么类型的格式,请解释这一行,这可能会对我有所帮助。Thanks@archana我认为如果([self.folderCheckArray containsObject:[NSString stringWithFormat:@“%@”,theSearchBar.text]])这对您不起作用,因为它需要整个字符串,即数组包含但正在输入子字符串的对象,您可以按照我的第二个答案进行操作,我相信这会对您有所帮助。