Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        3年前关闭。
                                                                                            
                
        
我有一个NSPredicate类似于一个How To Implement Search Bar in iOS 7 Using Storyboard

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
    searchResults = [recipes filteredArrayUsingPredicate:resultPredicate];
}


我正在尝试使用%@抓住self.searchText将其传递到下一个视图;

但这似乎不起作用。

如何引用NSPredicate的%@进行查询?

塞古:

if ([segue.identifier isEqualToString:@"Twotwo"]) {

    Two *two2 = [segue destinationViewController];
    two2.searchText = self.searchText;`


并在viewDidLoad中

    _searchText=self.searchText;


编辑:

基本上,我的目标是从TestViewController获得searchText字符串,以在segue上传递给Two。

TestViewController.h

@property(nonatomic, copy) NSString *searchText;


TestViewController.m

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"(one beginswith[c] %@) OR (two beginswith[c] %@) OR (three beginswith[c] %@)", searchText, searchText, searchText];
    searchResults = [cdh filteredArrayUsingPredicate:resultPredicate]; }


二小时

@property(nonatomic, copy) NSString *searchText;

最佳答案

您根本不应该使用谓词,并且在问题中提到它会使所有人感到困惑。似乎您已经具有要存储的搜索词的属性,只是没有更新它。n您应该具有:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
    self.searchText = searchText;
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
    searchResults = [recipes filteredArrayUsingPredicate:resultPredicate];
}


看起来您可以删除viewDidLoad中的行(再次看起来很困惑,因为您直接访问实例变量的内存并将其设置为同一实例变量的getter返回的值...)

关于ios - 如何引用NSPredicate的“%@”进行查询? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38169118/

10-13 09:04