Ios 需要了解搜索栏的目标c代码
我正在学习下面的教程 遇到了这段代码,它的工作原理让我很困惑:Ios 需要了解搜索栏的目标c代码,ios,objective-c,Ios,Objective C,我正在学习下面的教程 遇到了这段代码,它的工作原理让我很困惑: - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope { // find all the words wich begin with the letter: NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
// find all the words wich begin with the letter:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", searchText];
// NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"contains[c] %@", searchText];
self.searchResults = [self.array filteredArrayUsingPredicate:predicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
// update the tableview
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
我不明白的是:
scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
这就是我所理解的,我试图研究它,我知道范围是一个参数。但我现在需要的只是一些解释,这是一个复合语句。这样写会让人更难理解 我倾向于使用更简单的语句和临时变量,以使代码更容易理解。(由于可以在中间步骤之间设置断点并检查这些临时变量,因此调试也更容易。) 原件:
[self
filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:
[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
可以这样分解:
//Get the search bar's array of button titles
NSArray *titlesArray =
[self.searchDisplayController.searchBar scopeButtonTitles];
//Get the index of the selected scope button
NSInteger index =
[self.searchDisplayController.searchBar selectedScopeButtonIndex];
//Fetch the string at that index.
NSString *scopeString = titlesArray[index];
//Now assign the string.
[self
filterContentForSearchText: searchString
scope: scopeString];
看看你能不能把那句话翻译成Swift。如果没有,请发回问题
(我不熟悉原始代码中使用的几个函数。我只是在分析您发布的Objective-C代码的基础上,对正在发生的事情做一些假设。)好吧,您不能孤立地说。你引用了半句话。行是:
[self-filterContentForSearchText:searchString作用域:[[self.searchDisplayController.searchBar ScopeButtonTiles]对象索引:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]代码>你不能孤立地拿一块去问它;单凭它自己是没有意义的。你的问题就像在问,“用howdy这个词来说,wdy是什么?”