在 Xcode 的文本视图中处理多个 txt 文件的最佳方法是什么?

发布于 2024-11-05 15:13:13 字数 2119 浏览 0 评论 0原文

这是我的想法。我有大约 100 个 txt 文件,需要在用户单击工具栏中的下一个按钮后在文本视图中显示。下一个按钮也会触发随机数,因此文件将随机显示。我已通过生成随机数并使用“case”根据该数字分配文件来完成下面的代码。下面的示例使用 4 个 txt 文件,如果我有 100 个 txt 文件怎么办?有没有通用的方法来开发代码,而不是我必须写到“case”99? 请指教。

- (IBAction)Next:(id)sender {

// generate random number   
    int randomnumber = (arc4random() % (3));

//assign the number based on file number 
    switch (randomnumber) {
        case 0:
        {
            NSString *filePath=[[NSBundle mainBundle] pathForResource:@"file_no_1" ofType:@"txt"];
            NSString *myText= [NSString stringWithContentsOfFile:filePath
                                                        encoding:NSUTF8StringEncoding
                                                           error:nil];
            TextView.text=myText;
        }
            break;
        case 1:
        {
            NSString *filePath=[[NSBundle mainBundle] pathForResource:@"file_no_2" ofType:@"txt"];
            NSString *myText= [NSString stringWithContentsOfFile:filePath
                                                        encoding:NSUTF8StringEncoding
                                                           error:nil];
            TextView.text=myText;
        }
            break;
        case 2:
        {
            NSString *filePath=[[NSBundle mainBundle] pathForResource:@"file_no_3" ofType:@"txt"];
            NSString *myText= [NSString stringWithContentsOfFile:filePath
                                                        encoding:NSUTF8StringEncoding
                                                           error:nil];
            TextView.text=myText;
        }
            break;
        case 3:
        {
            NSString *filePath=[[NSBundle mainBundle] pathForResource:@"file_no_4" ofType:@"txt"];
            NSString *myText= [NSString stringWithContentsOfFile:filePath
                                                        encoding:NSUTF8StringEncoding
                                                           error:nil];
            TextView.text=myText;
        }
            break;

        default:
            break;
    }


}

Here is the idea. I have like 100 txt files and need to display in text view after the user click on the next button in the toolbar. The next button will also trigger random number so the file will be display randomly. I have completed my code below by generate random number and use 'case' to assign the file based on the number. Below sample is using 4 txt files, what about if I have 100 txt files. Is there any generic way to develop the code instead that I have to write until 'case' 99?
Please advise.

- (IBAction)Next:(id)sender {

// generate random number   
    int randomnumber = (arc4random() % (3));

//assign the number based on file number 
    switch (randomnumber) {
        case 0:
        {
            NSString *filePath=[[NSBundle mainBundle] pathForResource:@"file_no_1" ofType:@"txt"];
            NSString *myText= [NSString stringWithContentsOfFile:filePath
                                                        encoding:NSUTF8StringEncoding
                                                           error:nil];
            TextView.text=myText;
        }
            break;
        case 1:
        {
            NSString *filePath=[[NSBundle mainBundle] pathForResource:@"file_no_2" ofType:@"txt"];
            NSString *myText= [NSString stringWithContentsOfFile:filePath
                                                        encoding:NSUTF8StringEncoding
                                                           error:nil];
            TextView.text=myText;
        }
            break;
        case 2:
        {
            NSString *filePath=[[NSBundle mainBundle] pathForResource:@"file_no_3" ofType:@"txt"];
            NSString *myText= [NSString stringWithContentsOfFile:filePath
                                                        encoding:NSUTF8StringEncoding
                                                           error:nil];
            TextView.text=myText;
        }
            break;
        case 3:
        {
            NSString *filePath=[[NSBundle mainBundle] pathForResource:@"file_no_4" ofType:@"txt"];
            NSString *myText= [NSString stringWithContentsOfFile:filePath
                                                        encoding:NSUTF8StringEncoding
                                                           error:nil];
            TextView.text=myText;
        }
            break;

        default:
            break;
    }


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月光色 2024-11-12 15:13:13
- (IBAction)Next:(id)sender 
{
  //generate random number - result is a range of 0-99   
  int randomnumber = (arc4random() % 100);
  //render a filename based on that number ("file_no_1" - "file_no_100")
  NSString *fileName = [NSString stringWithFormat:@"file_no_%d", randomnumber + 1];
  //show the filename within our console
  NSLog(@"current filename:%@", fileName);
  //render a complete file-path out of our filename, the main-bundle and the file-extension
  NSString *filePath=[[NSBundle mainBundle] pathForResource:fileName ofType:@"txt"];
  //fetch the text content from that file
  NSString *myText= [NSString stringWithContentsOfFile:filePath
                                                        encoding:NSUTF8StringEncoding
                                                           error:nil];
  //hand that text over to our textview
  TextView.text=myText;

 //done - yet another dude got his homework done by someone else...
 //...damn lucky that dude, as I had such a pleasant day
}
- (IBAction)Next:(id)sender 
{
  //generate random number - result is a range of 0-99   
  int randomnumber = (arc4random() % 100);
  //render a filename based on that number ("file_no_1" - "file_no_100")
  NSString *fileName = [NSString stringWithFormat:@"file_no_%d", randomnumber + 1];
  //show the filename within our console
  NSLog(@"current filename:%@", fileName);
  //render a complete file-path out of our filename, the main-bundle and the file-extension
  NSString *filePath=[[NSBundle mainBundle] pathForResource:fileName ofType:@"txt"];
  //fetch the text content from that file
  NSString *myText= [NSString stringWithContentsOfFile:filePath
                                                        encoding:NSUTF8StringEncoding
                                                           error:nil];
  //hand that text over to our textview
  TextView.text=myText;

 //done - yet another dude got his homework done by someone else...
 //...damn lucky that dude, as I had such a pleasant day
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文