Im learning swift and am having a problem Iterating through an array. Here is what I'm trying to do:
我正在学习swift并且遇到问题迭代通过数组。这是我正在尝试做的事情:
func orderStringByOccurence(stringArray: [String]) -> [String: Int]{
var stringDictionary: [String: Int] = [:]
for i in 0...stringArray.count {
if stringDictionary[stringArray[i]] == nil {
stringDictionary[stringArray[i]] = 1
stringDictionary
} else {
stringDictionary[stringArray[i]]! += 1
}
}
return stringDictionary
}
I don't get an error until I try to call this function. Then I get this error:
在我尝试调用此函数之前,我没有收到错误。然后我收到这个错误:
EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)
EXC_BAD_INSTRUCTION(代码= EXC_1386_INVOP,子代码= 0x0)
I have tried debugging and found that i get the same error when i try this:
我试过调试,发现我尝试这个时遇到同样的错误:
for i in 0...arrayFromString.count{
print(arrayFromString[i])
}
So how do I iterate through this array? Thanks for helping out a new
那么如何遍历这个数组呢?感谢您帮助解决新问题
3 个解决方案
#1
12
You need to change
你需要改变
for i in 0...arrayFromString.count
to
for i in 0..<arrayFromString.count
As it is now, you iterate through the array and then one past the end.
就像现在一样,你遍历数组,然后遍历结束。
You can also use a different style of for loop, which is perhaps a little better:
你也可以使用不同风格的for循环,这可能会更好一些:
func orderStringByOccurence(stringArray: [String]) -> [String: Int] {
var stringDictionary: [String: Int] = [:]
for string in stringArray {
if stringDictionary[string] == nil {
stringDictionary[string] = 1
} else {
stringDictionary[string]! += 1
}
}
return stringDictionary
}
Also, you can simplify your logic a bit:
此外,您可以稍微简化您的逻辑:
for string in stringArray {
stringDictionary[string] = stringDictionary[string] ?? 0 + 1
}
#2
3
A few more approaches:
还有一些方法:
let array = ["1", "2", "3"]
You can use forEach
with trailing closure syntax:
您可以将forEach与尾随闭包语法一起使用:
array.forEach { item in
print(item)
}
You can use the $0
shorthand:
您可以使用$ 0速记:
array.forEach {
print($0)
}
And if you need the indexes, you can use enumerate()
:
如果需要索引,可以使用enumerate():
array.enumerate().forEach { itemTuple in
print("\(itemTuple.element) is at index \(itemTuple.index)")
}
#3
2
It seems you're out of index. A more swift-like approach would be in my opinion not to use the count but to do range-based.
看来你已经没有索引了。在我看来,更快速的方法是不使用计数而是基于范围。
var stringArray = ["1", "2", "3"]
for string in stringArray
{
print(string)
}
#1
12
You need to change
你需要改变
for i in 0...arrayFromString.count
to
for i in 0..<arrayFromString.count
As it is now, you iterate through the array and then one past the end.
就像现在一样,你遍历数组,然后遍历结束。
You can also use a different style of for loop, which is perhaps a little better:
你也可以使用不同风格的for循环,这可能会更好一些:
func orderStringByOccurence(stringArray: [String]) -> [String: Int] {
var stringDictionary: [String: Int] = [:]
for string in stringArray {
if stringDictionary[string] == nil {
stringDictionary[string] = 1
} else {
stringDictionary[string]! += 1
}
}
return stringDictionary
}
Also, you can simplify your logic a bit:
此外,您可以稍微简化您的逻辑:
for string in stringArray {
stringDictionary[string] = stringDictionary[string] ?? 0 + 1
}
#2
3
A few more approaches:
还有一些方法:
let array = ["1", "2", "3"]
You can use forEach
with trailing closure syntax:
您可以将forEach与尾随闭包语法一起使用:
array.forEach { item in
print(item)
}
You can use the $0
shorthand:
您可以使用$ 0速记:
array.forEach {
print($0)
}
And if you need the indexes, you can use enumerate()
:
如果需要索引,可以使用enumerate():
array.enumerate().forEach { itemTuple in
print("\(itemTuple.element) is at index \(itemTuple.index)")
}
#3
2
It seems you're out of index. A more swift-like approach would be in my opinion not to use the count but to do range-based.
看来你已经没有索引了。在我看来,更快速的方法是不使用计数而是基于范围。
var stringArray = ["1", "2", "3"]
for string in stringArray
{
print(string)
}