我正在尝试遍历一系列与此类似的字符
//Example.txt 0 X X X 0 0 X 0 X 0 0 0 X 0 0 0 X 0 X 0 0 X X X 0我想能够分辨出X和0之间的区别。 我目前使用的代码导致无限循环运行,然后我的应用程序从未完成构建。
let path = Bundle.main.path(forResource: "Example.txt", ofType: nil) do { let fileContents = try String(contentsOfFile:path!, encoding: String.Encoding.utf8) let lines = fileContents.components(separatedBy: "\n") for row in 0..<lines.count { let items = lines[row].components(separatedBy: " ") for column in 0..<items.count { print(column,row) } } } catch { print("Error loading map") }它会很好地对整个.txt文档进行迭代,它不会完成构建和运行,它会停留在白色的加载屏幕上。 我认为这与文本的编码有关
我如何修复无限循环错误,并告诉我打算在后面添加的不同字符之间的区别。
-编辑-
我想出了如何测试角色来说明他们之间的差异 -
for row in 0..<lines.count { let items = lines[row].components(separatedBy: " ") for column in 0..<items.count { if items[1] == "0" { print("0") } else { print("X") } } }问题在于它将文档中的每一行视为一个数组,因此测试中的项目行出现数组越界错误。
I am trying to iterate through a series of characters similar to this
//Example.txt 0 X X X 0 0 X 0 X 0 0 0 X 0 0 0 X 0 X 0 0 X X X 0I want to be able to tell the difference between the X's and the 0's. The code I am currently using cause an infinite loop to run and then my app never finishes building.
let path = Bundle.main.path(forResource: "Example.txt", ofType: nil) do { let fileContents = try String(contentsOfFile:path!, encoding: String.Encoding.utf8) let lines = fileContents.components(separatedBy: "\n") for row in 0..<lines.count { let items = lines[row].components(separatedBy: " ") for column in 0..<items.count { print(column,row) } } } catch { print("Error loading map") }It will Iterate through the .txt document perfectly fine, it just won't finish building and running, it gets stuck on the white loading screen. I think it has something to do with the encoding of the text
How can I fix the infinite loop bug, and tell the difference between the different characters that I intend to add in later.
-Edit-
I figured out how to test the characters to tell the difference between them using-
for row in 0..<lines.count { let items = lines[row].components(separatedBy: " ") for column in 0..<items.count { if items[1] == "0" { print("0") } else { print("X") } } }the problem is that it treats each row in the document as an array so the items line when testing reaches an array out-of-bounds error.
最满意答案
你没有在内部使用“列”变量。 事实上,在每一次迭代中,您总是将“items”数组的第二个元素( items[1] )与“0”进行比较 。 如果items数组少于2个元素,则会导致出界错误。
也许你应该迭代如下(我建议使用“for in”循环):
for line in lines { let items = line.components(separatedBy: " ") for item in items { if item == "0" { print("0") } else { print("X") } } }虽然我不明白你的“if else”,但如果文件只能包含“0”或“X”,而你只是将其可视化,那么你可以直接print(item) 。
You are not using the "column" variable inside your inner for. In fact, on every iteration, you're always comparing the second element of the "items" array (items[1]) against "0". If the items array has less than 2 elements, it will cause an out-of-bounds error.
Perhaps you should iterate as follows (I suggest using "for in" loops):
for line in lines { let items = line.components(separatedBy: " ") for item in items { if item == "0" { print("0") } else { print("X") } } }Although I don't understand your "if else", if the file can only contain either "0" or "X" and you're only visualizing it, you might as well just print(item) instead.
更多推荐
发布评论