Array Index Out of Range?

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

Accepted Reply

You are attempting to initialize elements in your destination array that don't exist yet. Rather than:

myArray[i] = getArray[i]

do:

myArray.append(getArray[i]


There is also an array member function that will allow you to 'append' the contents of another array, but i'm unsure of what it is from memory, probaby myArray.appendFrom().. but I'm unsure.

Replies

You are attempting to initialize elements in your destination array that don't exist yet. Rather than:

myArray[i] = getArray[i]

do:

myArray.append(getArray[i]


There is also an array member function that will allow you to 'append' the contents of another array, but i'm unsure of what it is from memory, probaby myArray.appendFrom().. but I'm unsure.

You have declared array as a constant.

So, you cannot change it.

You need also call getArray as a function with getArray(), and then select the index.

You should write:


var 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.append(getArray()[i])

}


If you wanted anyway to do it your way, you should first create an array of the right size:


var myArray: [String] = Array(repeating: "", count: getArray().count)

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 {<br="">    myArray[i] = getArray()[i]
}



or simply:


var myArray: [String] = Array(repeating: "", count: getArray().count)


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 val in getArray() {

myArray.append(val)

}

If I understood that correctly, you want the elements of the getArray () method to be equal to the arraylist myArray.


That is my suggestion:


//Array
var myArray: [String] = []

//Set the data
myArray = getArray()

//Method
 func getArray() -> [String] {
        
      return ["a","b","c"]
        
 }