获取以特定单词开头的txt文件名

时间:2012-01-31 06:16:31

标签: objective-c xcode textfield

参考这个问题   Get all file names starting with a prefix from Resource folder 在这里它说'前缀'这个词。但在我的应用程序中,我需要从文本字段中给出的单词开始的所有文件名到表中。我该如何实现呢?我尝试使用“%@”,然后给出了文本字段内容。但它不起作用。但相反,如果我给出类似“h”的东西,它会显示所有以'h'开头的文件名。请帮忙回复。

-(IBAction)Do_Search:(id)sender
{    
    files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:
             [[NSBundle mainBundle] bundlePath] error:nil];
    search_results_array = [files filteredArrayUsingPredicate:
                            [NSPredicate predicateWithFormat:@"self BEGINSWITH[cd] 'h'"]];
    NSLog(@"%@", search_results_array);
    int search_res_count=[search_results_array count];
    NSLog(@"Array has %d results...",search_res_count);
    [search_results_array retain];
    [Result_table reloadData];
    //till this part it works fine. I got all file names starting with 'h' in table. now what i need is i should get all file names starting with the word given in the search bok

    NSString *Search_string=Search_song.text;
    NSString *filePath = [[NSBundle mainBundle] pathForResource:Search_string ofType:@"txt"];  
    if (filePath) {  
        NSString *myText = [NSString stringWithContentsOfFile:filePath];  
        if (myText) {  
            song_lyrics.text= myText;  
            //what i did here is if i give a complete file name in the search field (textfield). i am getting the complete file contents inside a text view
        }  
    }  
}

我知道代码现在的顺序不正确。我正在尝试这种自上而下和自下而上的方法..

2 个答案:

答案 0 :(得分:0)

PL。改变行

search_results_array = [files filteredArrayUsingPredicate:
                        [NSPredicate predicateWithFormat:@"self BEGINSWITH[cd] 'h'"]];

如下

search_results_array = [files filteredArrayUsingPredicate:
                        [NSPredicate predicateWithFormat:@"self BEGINSWITH[cd] '%@'",searchBoxText]];

答案 1 :(得分:0)

它是如此简单......而不是'h'我从那里删除''并给了%@这是

search_results_array = [files filteredArrayUsingPredicate:
                        [NSPredicate predicateWithFormat:@"self BEGINSWITH[cd] %@",Search_string]];

并解决了我的问题......谢谢大家:)