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
Reading file line by line in iOS SDK
提问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 array1
and line 2, line 4, line 6,...
go into array2
.
Each element of arrays stores one line.
我想从文件中读取两个字符串数组,以便line1, line3, line 5,...
进入array1
和line 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 个数组中