【问题标题】:Getting the txt file names starting with a particular word获取以特定单词开头的 txt 文件名
【发布时间】:2012-01-31 06:16:31
【问题描述】:

参考这个问题 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
        }  
    }  
}

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

【问题讨论】:

  • 你能给出你试图实现这个的代码吗?
  • 给出代码。抱歉,我试图将数据加载到表格中。

标签: objective-c xcode textfield


【解决方案1】:

Pl。换行

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

如下

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

【讨论】:

  • 数组是空的,那么这里的 searchBoxText 是我的 Search_song。
【解决方案2】:

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

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

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

【讨论】:

  • 这只能在 20 小时后完成……我会这样做……亲爱的,请稍候:)
  • 二十?可以发誓它比那少得多......好吧,抱歉让你匆忙
  • 他们说我只能在 20 小时后接受我的答案作为正确答案......我正在等待......现在还有 20 分钟 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-10
  • 1970-01-01
  • 1970-01-01
  • 2022-10-12
  • 1970-01-01
相关资源
最近更新 更多