在 Xcode 的文本视图中处理多个 txt 文件的最佳方法是什么?
这是我的想法。我有大约 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)