Home > other >  Verify and swap elements swift
Verify and swap elements swift

Time:02-15

Sorry for the beginner question and my bad english.

I need to make a function in my class Team that will check my array to see if the player exists, if doesn't, it will return an error, if it exists, it will make an swap between the two array.

Example: I have an array with starters and reserve players, players 1,2 and 3 are starters, and players 4,5 and 6 are substitutes.

so when I want to do a player swap, I can use something like: Team.swapfunc(player1, player4) or something similar

My example code:

class SoccerPlayers {
let name: String
let number: Int
init(name: String, number: Int) {

    self.name = name
    self.number = number
}
}
class Team {

    var nameTeam: String

    // Array of players
    var startingPlayers:[SoccerPlayers] = []
    var reservePlayers:[SoccerPlayers] = []

    init(nameTeam:String, startingPlayers: [SoccerPlayers], reservePlayers: [SoccerPlayers]) {
        self.nameTeam = nameTeam
        self.startingPlayers = startingPlayers
        self.reservePlayers = reservePlayers
    }
}

var player1: SoccerPlayers = SoccerPlayers(    
    name: "Andre",
    number: 1
)

var player2: SoccerPlayers = SoccerPlayers(    
    name: "Joao",
    number: 2
)

var player3: SoccerPlayers = SoccerPlayers(    
    name: "Matheus",
    number: 3
)

var player4: SoccerPlayers = SoccerPlayers(    
    name: "Junior",
    number: 4
)

var player5: SoccerPlayers = SoccerPlayers(    
    name: "Fabio",
    number: 5
)

var player6: SoccerPlayers = SoccerPlayers(
    name: "Paulo",
    number: 6
)

let team1 = Team(nameTeam:"YourTeam", startingPlayers:[player1, player2, player3], reservePlayers:[player4, player5, player6])
 }

obs: this code is only for study, not a real working project

CodePudding user response:

Let's break this down, what we need to do is to

  • Check if players exists in the corresponding array
  • Remove them from the array
  • Insert them into the other array

To check if a player exist we want to be able to compare two players, we can either do that manually or make the player type correspond to Equatable and also decide what makes two player equal, here I decided that it is the number that uniquely defines a player so we are using that.

struct SoccerPlayer: Equatable { // Conform to Equatable
    let name: String
    let number: Int

    // This function will automatically be used whenever some other function want's to check if two players are equal
    static func == (lhs: SoccerPlayer, rhs: SoccerPlayer) {
        lhs.number == rhs.number
    }
}

Note that I have changed from class to struct because that is what is recommended for this kind of data/functionality. I also change the name to SoccerPlayer since each instance holds a single player

In the swap function we need to check for a player and then remove it from the array, looking at the Array type we see it has a remove function that takes an index (position in the array) as an argument so we want to get the index of each player first.

The function firstIndex(of:) is perfect here since it returns an index or nil if nothing was found so we can also use it for our validation.

let playerIndex = startingPlayers.firstIndex(of: player)

here playerIndex will be nil if the array doesn't contain the player, also not that pass the player as argument to the function and this can be done since it conforms to Equatable

To perform the check and get the result in one go we use a guard statement. And once we have the index we can remove the players from one array and add them to the other. Here is the full swap function that belongs to Team

 mutating func swap(player: SoccerPlayer, substitute: SoccerPlayer) -> Bool {
    guard let playerIndex = startingPlayers.firstIndex(of: player),
          let substituteIndex = reservePlayers.firstIndex(of: substitute) else {
              return false
          }

    // Use the indices to remove players
    startingPlayers.remove(at: playerIndex)
    reservePlayers.remove(at: substituteIndex)

    // Add them to the other array
    startingPlayers.append(substitute)
    reservePlayers.append(player)

    return true
}

Note that I have also changed Team to a struct and that the mutating keyword is used to tell the compiler we are updating the struct.

I didn't really understand what you meant by returning an error so I simply return a boolean instead.

As an extra check you could test that each player doesn't exists in the array we are going to add it to

  • Related