Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Change Values while Iterating Over Subarrays in Swift

What works in Python doesn’t want to work in Swift.

Given is an array with subarrays containing numbers. During iteration, as soon as the condition exists that the element in question contains a certain value at position 0, I want to change the other values in this same subarray.

var coordinatesList = [[0, 1, 1, 1], [1, 12, 17, 23], [2, 81, 29, 66], [3, 41, 20, 94]]



for i in coordinatesList {
    if i[0] == 6 {
        i[3] = 5
    }
}

However, my intention fails, because I get the error message that the element i is a let.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

An error message with which I can do nothing at all and do not know how to deal with it.

>Solution :

You can iterate over the indices of the outer array. Then you can read and write the inner arrays using the indices:

for index in coordinatesList.indices {
    if coordinatesList[index][0] == 6 {
        coordinatesList[index][3] = 5
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading