通过字符串迭代并测试字符的差异(Iterate through Strings and test for differences in characters)

编程入门 行业动态 更新时间:2024-10-28 17:18:44
通过字符串迭代并测试字符的差异(Iterate through Strings and test for differences in characters)

我正在尝试遍历一系列与此类似的字符

//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 0

I 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.

更多推荐

本文发布于:2023-08-03 05:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1384088.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   字符   差异   迭代   测试

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!