How to Iterate through an array in swift

A few more approaches:

let array = ["1", "2", "3"]

You can use forEach with trailing closure syntax:

array.forEach { item in
    print(item)
}

You can use the $0 shorthand:

array.forEach {
    print($0)
}

And if you need the indexes, you can use enumerate():

array.enumerate().forEach { itemTuple in
    print("\(itemTuple.element) is at index \(itemTuple.index)")
}

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:

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
}

Update - For the sake of completeness, I thought I'd add a reduce example here as well. Note that as of Swift 5.1 return statements in single line functions can be implied (SE-0255).

func orderStringByOccurence(stringArray: [String]) -> [String: Int] {
    stringArray.reduce([:]) { result, string in result.merging([string: 1], uniquingKeysWith: +)}
}

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)
}

Tags:

Arrays

Swift