且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

读取文本文件的特定部分

更新时间:2022-12-20 11:53:19

您可以将整个文件读取为 NSString ,然后使用 componentsSeparatedByString:@"\ n将其拆分为一个数组",保留它的内存,并在需要时返回该数组的随机元素.如果只有几百行(而不是几百万行),那么使用额外的内存就不会成为问题.

You could read the whole file into an NSString, then split it into an array with componentsSeparatedByString:@"\n", keep that it memory, and return a random element of the array when needed. If there are just hundreds (and not, say, millions) of lines, the additional memory used should not be an issue.