How do I remove a string from an array if I remove the string from a different array?

I have two arrays of strings and two table views. In one table view I am using swipe to remove feature. Both arrays hold the same data. When I swipe to remove, I want to remove the same string from the other array also. The strings will have different indexes in the arrays and one array will have a prefix and a suffix. How do I do this?



var myArray1: [String] = ["1. Adam, Blazers", "2. Jack, Dare Devils", "3. Steven, Tigers", "4. Donald, Sharks",
                          "5. Aaron, Cyclones"]
var myArray2: [String] = ["1. Donald, Sharks - QB", "2. Aaron, Cyclones - RB", "3. Adam, Blazers - WR",
                          "4 Jack, Dare Devils - WR", "5. Steven, Tigers - TE"]


I swipe the row in the table that holds "3. Adam, Blazers - WR" from myArray2. I want to remove the string from the myArray1 that holds the string "1. Adam, Blazers". In other words, I swipe the row in myArray2 at index 2 and I want it to remove the string in myArray1 at index 1. How do I do that?

As a general comment, may be you should have a single array with the 2 elements:

either with a struct:

struct Player {
     var shortName: String
     var longName: String

}


or simply with typeAlias

typealias Player = (shortName: String, longName: String)

and access as:

        let player: Player = ("1. Adam, Blazers", "3. Adam, Blazers - WR")
        print(player.shortName)


Then your arrays are merged in a single one:

var myArray: [Player] = [
          ("1. Adam, Blazers", "3. Adam, Blazers - WR"),
          ("2. Jack, Dare Devils",  "4 Jack, Dare Devils - WR"),
          ("3. Steven, Tigers", "5. Steven, Tigers - TE"),
          ("4. Donald, Sharks", "1. Donald, Sharks - QB"),
          ("5. Aaron, Cyclones", "2. Aaron, Cyclones - RB")
          ]

//  If you need myArray1 and myArray2:
var myArray1 : [String] {
    myArray.map { $0.shortName }.sorted()
}
var myArray2 : [String] {
    myArray.map { $0.longName }.sorted()
}

print(myArray1)
print(myArray2)


Here the results of testing :


print(myArray1)
print(myArray2)

myArray.remove(at: 2) // That's what you do when you want to remove at pos 2 in myArray2
print("After removing item 2 in myArray")
print(myArray1)
print(myArray2)


["1. Adam, Blazers", "2. Jack, Dare Devils", "3. Steven, Tigers", "4. Donald, Sharks", "5. Aaron, Cyclones"]

["1. Donald, Sharks - QB", "2. Aaron, Cyclones - RB", "3. Adam, Blazers - WR", "4 Jack, Dare Devils - WR", "5. Steven, Tigers - TE"]

After removing item 2 in myArray

["1. Adam, Blazers", "2. Jack, Dare Devils", "4. Donald, Sharks", "5. Aaron, Cyclones"]

["1. Donald, Sharks - QB", "2. Aaron, Cyclones - RB", "3. Adam, Blazers - WR", "4 Jack, Dare Devils - WR"]


----------------------------------


If you want to stay with your design (I do not recommend),

before deleting in myArray2

extract the "short name",

- with regex, looking for string between . and -


You can also use


var myArray1: [String] = ["1. Adam, Blazers", "2. Jack, Dare Devils", "3. Steven, Tigers", "4. Donald, Sharks", "5. Aaron, Cyclones"]
var myArray2: [String] = ["1. Donald, Sharks - QB", "2. Aaron, Cyclones - RB", "3. Adam, Blazers - WR", "4 Jack, Dare Devils - WR", "5. Steven, Tigers - TE"]

let testString = myArray2[1]     // "2. Aaron, Cyclones - RB"

let startIndex = testString.index(testString.firstIndex(of: ".")!, offsetBy: 2)
let endIndex = testString.index(testString.firstIndex(of: "-")!, offsetBy: -2)
let extract = String(testString[startIndex...endIndex])

let pos = myArray1.firstIndex{$0.contains(extract)} ?? -1
print(pos)

Then:

  • delete in myArray2
  • search for the short name in myArray1 (the code above)
  • remove at this position pos in myArray1


As you see, the first solution is far simpler.


Question: How did you define the leading index in each array ? When you remove one item, do you keep the original index (what I assumed here) or do you recompute to have a continuous serie ? But then, how do you recompute for myArray2 ?

How do I remove a string from an array if I remove the string from a different array?
 
 
Q