Read lines from a large text file in Swift until the new line is empty: Swift path

I have the following text file structure (the text file is quite large, about 100,000 lines):

A|a1|111|111|111 B|111|111|111|111 A|a2|222|222|222 B|222|222|222|222 B|222|222|222|222 A|a3|333|333|333 B|333|333|333|333 ... 

I need to extract a piece of text associated with this key. For example, if my key is A | a2, I need to save the following as a string:

 A|a2|222|222|222 B|222|222|222|222 B|222|222|222|222 

For my C ++ and Objective-C projects, I used the C ++ getline function as follows:

 std::ifstream ifs(dataPathStr.c_str()); NSString* searchKey = @"A|a2"; std::string search_string ([searchKey cStringUsingEncoding:NSUTF8StringEncoding]); // read and discard lines from the stream till we get to a line starting with the search_string std::string line; while( getline( ifs, line ) && line.find(search_string) != 0 ); // check if we have found such a line, if not report an error if( line.find(search_string) != 0 ) { data = DATA_DEFAULT ; } else{ // we need to form a string that would include the whole set of data based on the selection dataStr = line + '\n' ; // result initially contains the first line // now keep reading line by line till we get an empty line or eof while(getline( ifs, line ) && !line.empty() ) { dataStr += line + '\n'; // append this line to the result } data = [NSString stringWithUTF8String:navDataStr.c_str()]; } 

As I make a project in Swift, I try to get rid of getline and replace it with something like β€œCocoaish”. But I can not find a good Swift solution to solve the above problem. If you have an idea, I would really appreciate it. Thanks!

+5
source share
1 answer

Using the StreamReader class from Read the file / URL in the Swift line , you can do it like Swift as follows:

 let searchKey = "A|a2" let bundle = NSBundle.mainBundle() let pathNav = bundle.pathForResource("data_apt", ofType: "txt") if let aStreamReader = StreamReader(path: pathNav!) { var dataStr = "" while let line = aStreamReader.nextLine() { if line.rangeOfString(searchKey, options: nil, range: nil, locale: nil) != nil { dataStr = line + "\n" break } } if dataStr == "" { dataStr = "DATA_DEFAULT" } else { while let line = aStreamReader.nextLine() { if countElements(line) == 0 { break } dataStr += line + "\n" } } aStreamReader.close() println(dataStr) } else { println("cannot open file") } 
+9
source

Source: https://habr.com/ru/post/1206532/


All Articles