Hi Apple Dev Community,
I am rather new to Swift. So please bare with me if the following question sounds dumb..
I don't understand why I am getting an Index out of range error when I know for sure that I am interating within the bounds of the array.
Please see below code:
let myArray: [String] = []
func getArray() -> [String] {
// In real life, I don't know what the array is; but for convenience's sake, suppose it is [a,b,c]
return ["a","b","c"]
}
for i in 0..<getArray().count {
myArray[i] = getArray[i]
}
That is it. Basically, I am trying to initialize the elements of myArray by copying another array (in this case returned by getArray( ).
As you can see, the above task is very simple, straightforward, and short. I don't understand where it's failing.
If my understanding is correct (a big assumption since I am new to Swift), the getArray( ) has 3 elements, so getArray( ).count should return 3. And because the range specified is "<", it does NOT contain 3, so 0..<3 should be [0,1,2]...so everything should work fine?
Clearly, I am missing something here? Anyone care to shed some light on this? I don't know why code as innocuous as this would cause any problems.
FWIW, I am using Xcode 10.1 on Mac OS 10.14.5.
Thanks in advance,
KC