Filepath with a string Xcode
I am trying to get random text files from Resources folder to be shown in Textview. I could not do this because i could not get that 1 in pathForResource:@"1" increment.开发者_StackOverflow中文版 How can I increment that 1 after @. Thanks.
Note: I am trying to use stringb there but it does not work.
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;
}
}
You need to increment an integer variable and create a string using it:
static int number = 1;
NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%i",number++] ofType:@"txt"];
...
I assumed that this method is the only one which will access the variable. If not you will either need to make number
an instance variable or a global variable.
精彩评论