开发者

What is the best way to handle multiple txt file to be view in Text view in Xcode?

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;
    }


}


- (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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜