0
var word = "morning"
var arr = Array(word)
for s in 0...word.count {
        print(arr[s])
}

This will not print. Of course, if I substitute a number for s, the code works fine. Why will it not accept a variable in the array access braces? Is this peculiar to Swift?

I've spent a long time trying to figure this out, and it's nothing to do with s being optional.

Anyone understand this?

2
  • 2
    you problem is that your array indices is up to its count. Array indices start at zero not one. So you just need to use 0..<word.count, for i in word.indices or change your loop to for char in word { print(char) }. There is no need to create an array for that. Commented Jan 2, 2018 at 0:05
  • This is not peculiar to Swift. Array indexing in most languages start at 0. words.count is the one-past index Commented Jan 2, 2018 at 3:10

4 Answers 4

1

you are using inclusive range ... instead of ..<, so s goes from 0 to 7, not 0 to 6.

However, in arr the index goes from 0 to 6 because there are 7 characters.

Thus, when the program tries to access arr[7], it throws an index out of range error.

If you were coding on Xcode, the debugger would have told you that there is no arr[7].

As for the code, here is a better way to print every item in arr than using an index counter:

var word = "morning"
var arr = Array(word)

for s in arr {
    print(s)
}

This is called a "foreach loop", for each item in arr, it assigns it to s, performs the code in the loop, and moves on to the next item, assigns it to s, and so on.

When you have to access every element in an array or a collection, foreach loop is generally considered to be a more elegant way to do so, unless you need to store the index of a certain item during the loop, in which case the only option is the range-based for loop (which you are using).

Happy coding!

Sign up to request clarification or add additional context in comments.

Comments

0

When I run it, it prints the array then throws the error Fatal error: Index out of range. To fix this, change the for loop to:

for s in 0..<word.count {
    print(arr[s])
}

2 Comments

Brilliant! Wish it had shown me that fatal error. On 2 different online editors, I just got no output, or an exit code whose explanation I could not make sense of. Thank you.
Note even tough in this case it doesn't make a difference, you should use the array count not the word count. Another thing to consider is that not every collection starts at 0 index (i.e. ArraySlice), so you should iterate collections using its indices. for i in arr.indices
0

try this when you use a word to recognize size of Array your Array index start as 0 so array last index must be equal with your (word.count - 1)

var word = "morning"
    var arr = Array(word)
    for s in 0...(word.count-1) {
        print(arr[s])
    }

Comments

0

Basically avoid index based for loops as much as possible.

To print each character of a string simply use

var word = "morning"
for s in word { // in Swift 3 word.characters
    print(s)
}

To solve the index issue you have to use the half-open range operator ..< since indexes are zero-based.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.