1

I have an array.

var array_var: [String] = [
"https://www.filepicker.io/api/file/XXXXXXXXXXXXX/convert?fit=crop&w=200&h=200&rotate=exif",
"https://www.filepicker.io/api/file/XXXXXXXXXXXXXX/convert?fit=crop&w=320&h=300&rotate=exif",
"https://www.filepicker.io/api/file/XXXXXXXXXXXXXXXX",
"https://www.filepicker.io/api/file/XXXXXXXXXXXXXXXX"
]

So I just want to remove(exclude) the elements that contains the string convert?fit=crop from the array.

So how can we remove them by using Swift??

Deduplicator
  • 44,692
  • 7
  • 66
  • 118

2 Answers2

4

You can use filter method:

array_var = array_var.filter {
    $0.rangeOfString("convert?fit=crop") == nil
}
Kirsteins
  • 27,065
  • 8
  • 76
  • 78
0

A simple way to do it

var array_var: [String] = [
"https://www.filepicker.io/api/file/XXXXXXXXXXXXX/convert?     fit=crop&w=200&h=200&rotate=exif",
"https://www.filepicker.io/api/file/XXXXXXXXXXXXXX/convert?fit=crop&w=320&h=300&rotate=exif",
"https://www.filepicker.io/api/file/XXXXXXXXXXXXXXXX",
"https://www.filepicker.io/api/file/XXXXXXXXXXXXXXXX"
]

var newArray: [String] = []


for i in 0...array_var.count - 1 {
if array_var[i].rangeOfString("convert?fit=crop") != nil {

    newArray.append(array_var[i])


}
}
array_var = newArray
Abubakar Moallim
  • 4,903
  • 4
  • 13
  • 16