0

我正在尝试从 Resources 文件夹中获取随机文本文件以显示在 Textview 中。我不能这样做,因为我无法在 pathForResource:@" 1 " 增量中得到那个1 。我怎样才能在@之后增加那个1。谢谢。

注意:我正在尝试在那里使用 stringb 但它不起作用。

int b=(arc4random()%9)+1;

NSString *stringb = [NSString stringWithFormat:@"%d", b];


NSString *filePath = [[NSBundle mainBundle]pathForResource:@"1" ofType:@"txt"];  
if (filePath) {  
    NSString *myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    if (myText) {  
        textView1.text= myText;  
    }  
} 
4

1 回答 1

0

您需要增加一个整数变量并使用它创建一个字符串:

static int number = 1;
NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%i",number++] ofType:@"txt"];
...

我假设此方法是唯一可以访问该变量的方法。如果没有,您将需要创建number实例变量或全局变量。

于 2010-12-13T22:53:33.103 回答