xcode 在 iOS SDK 中逐行读取文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11218318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:41:56  来源:igfitidea点击:

Reading file line by line in iOS SDK

iosxcodesdk

提问by DavidNg

I have a texfile as follows:

我有一个 texfile 如下:

line1
line2
line3
line4
line5
 ....

I want to read from file into two arrays of string so that line1, line3, line 5,...go into array1and line 2, line 4, line 6,...go into array2. Each element of arrays stores one line.

我想从文件中读取两个字符串数组,以便line1, line3, line 5,...进入array1line 2, line 4, line 6,...进入array2. 数组的每个元素存储一行。

回答by borrrden

Step 1) Read file ([NSString stringWithContentsOfFile:encoding:error:])

步骤 1) 读取文件 ( [NSString stringWithContentsOfFile:encoding:error:])

Step 2) Split string ([NSString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]])

步骤 2) 拆分字符串 ( [NSString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]])

Step 3) Iterate over array and insert into your 2 arrays

步骤 3)迭代数组并插入到您的 2 个数组中