简体   繁体   English

使用分隔符nsstring读取文本文件

[英]read text file with delimiter nsstring

I have a text file like this 我有这样的文本文件

question1 question2 question3 问题1问题2问题3

I want to read only the first line into a nsstring 我想只读第一行到一个nsstring

after that I want to mark the first line so the next time, skip the first line, but I don't know how to do it, I have this code for read but give me only the third line 之后我想标记第一行,所以下次,跳过第一行,但我不知道该怎么做,我有这个代码供阅读,但只给我第三行

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"qa" ofType:@"txt"];  

NSString *myText = [NSString  stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];   
NSArray *lines = [myText componentsSeparatedByString:@"\n"];    
for(myText in lines)
{

    texview.text = myText;  

}


}  

Ok I try this code, Do not work, what I am Doing wrong? 好的,我试试这个代码,不要工作,我做错了什么?

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString * const kKey = @"OneTimeKey";
NSObject  *keyValue = [defaults  objectForKey:kKey];



NSString *filePath = [[NSBundle mainBundle] pathForResource:@"qa" ofType:@"txt"];  

NSString *myText = [NSString  stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];   
NSArray *lines = [myText componentsSeparatedByString:@"|"];    



if (keyValue==nil) {
    for(myText in lines)

    {

        preguntadia.text = myText;  

        [defaults setBool:YES forKey:kKey];

    }



}

The problem is that your "for" is executed really fast, so fast that you can only see when the last element of the array is set to your text view. 问题是你的“for”执行得非常快,速度太快,你只能看到数组的最后一个元素被设置为文本视图的时间。

What you can do is have a variable which specifies which one of the strings to show next and, with a timer, set a different string on your text view every number of seconds, so you can actually see the changes. 您可以做的是有一个变量,它指定下一个要显示的字符串,并且使用计时器,每隔几秒钟在文本视图中设置一个不同的字符串,这样您就可以实际看到更改。

Bonus, you can make it cycle with something like this: 奖金,你可以让它循环使用这样的东西:

-(void)updateText{
    [textView setText:[lines objectAtIndex:currentIndex]];
    currentIndex++;
    currentIndex = currentIndex % [lines count];
}

So when it reaches the last string, it shows the first again. 所以当它到达最后一个字符串时,它再次显示第一个字符串。

You'll need to store a line counter locally to keep track, I think. 我想你需要在本地存储一个行计数器以跟踪。 That is easily accomplished with something like this to save: 这很容易用这样的东西来保存:

[[NSUserDefaults standardDefaults] setInteger:lineCount forKey:@"lineCountKey"];

and fetch: 和获取:

int lineCount = [[NSUserDefaults standardDefaults] integerForKey:@"lineCountKey"];

and then just read and discard lineCount lines when you open the file. 然后在打开文件时读取并丢弃lineCount行。

Now, splitting the line you want into multiple pieces, you can use the NSString method componentsSeparatedByString: , but be careful. 现在,将您想要的行拆分成多个部分,您可以使用NSString方法componentsSeparatedByString: ,但要小心。 If your "questions" are actually multi-word questions, you'll need to pick a separator other than space. 如果您的“问题”实际上是多字的问题,那么您需要选择除空格之外的分隔符。 Vertical bar (|) might be a good choice, then you can do something like this: 竖条(|)可能是一个不错的选择,那么你可以这样做:

NSArray *questions = [line componentsSeparatedByString:@"|"];

NB: Code typed from memory; NB:从内存中输入的代码; not compiled. 没编译。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM