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 ?