0

I want to delete the strings from an array that do not contain the substring "originals". I tried this and it didn't work, it just printed the whole array. I need like the equivalent of a for i loop from Java.

for (position, imageURL) in imageURLs.enumerated() {
    if !imageURLs[position].contains("originals"){
        imageURLs.remove(at: position)
    }
}
print(imageURLs)
1
  • All you need is imageURLs = imageURLs.filter { $0.contains("originals") }. Commented Dec 26, 2022 at 23:55

1 Answer 1

1

Removing elements from a sequence while iterating through the sequence is often bug-prone. Consider removing from the collection directly with a predicate:

imageURLs.removeAll{ !$0.contains("originals") }
Sign up to request clarification or add additional context in comments.

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.