1

Here I have a class, Player, that has a variable of type, Sport, of which can be Basketball or Soccer. I'd like to be able to declare the type of Sport in the Player declaration. Any suggestions?

class Soccer : Sport {
    override var players : Int { get { return 11 } }
}

class Basketball : Sport {
    override var players : Int { get { return 5 } }
}

class Sport {

    var teamName: String
    var players: Int { get { return 0 } }

    init(teamName: String) {
        self.teamName = teamName
    }

}

class Player {
    let sport : Sport?

    init? (typeOfSport: Soccer, teamName: String) {
        self.sport = Soccer(teamName: teamName)
    }

    init? (typeOfSport: Basketball, teamName: String) {
        self.sport = Basketball(teamName: teamName)
    }
}

let me = Player(typeOfSport: Soccer(), teamName: "chelsea")

let him = Player(typeOfSport: Basketball(), teamName: "wizards")
Joshua Archer
  • 113
  • 3
  • 12

3 Answers3

1

You could also use an enum for this like this:

enum Sport {
    case Soccer (teamName : String)

    var players: Int {
        switch self{
            case .Soccer: return 11
            default: return 0
        }
    }
}

class Player {
    let sport: Sport?
    init? (s : Sport){
        self.sport = s
    }
}

Sport.Soccer (teamName: "Cambuur").players
Raymond
  • 3,630
  • 2
  • 19
  • 22
0

In this case I suggest for your example is this solution, I don't know if is the better way, but in my studies about OOP, I believe it is a cool way to do your example.

protocol Sport {
     func getTotalPlayers() -> Int
}

class Soccer: Sport {
      func getTotalPlayers() -> Int {
          return 11
      }
}

class Basketball: Sport {
     func getTotalPlayers() -> Int {
          return 5
     }
}

class Team {
    private var sport: Sport
    private var name: String

    init(sport:Sport, name:String) {
        self.sport = sport
        self.name = name
    }

    func getTeamName() -> String {
        return name
    }

    func getSport() -> Sport {
          return sport
    }
}

class Chelsea: Team {
    init() {
        super.init(sport: Soccer(), name: "Chelsea")
    }
}

class Wizards: Team {
    init() {
        super.init(sport: Basketball(), name: "Wizards")
    }
}

class Player {

    private var team: Team

    init(team: Team) {
        self.team = team
    }

    func getTeamName() -> String {
        return self.team.getTeamName()
    }  

    func getSport() -> Sport {
        return self.team.getSport()
    }
}

let me = Player(team: Chelsea())
let him = Player(team: Wizards())
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
ViTUu
  • 1,204
  • 11
  • 21
  • Fala Vitor, eu acho que nesse caso voce não precisa usar funções se você levar em conta que não precisa passar nenhum parametro. func getTotalPlayers() -> Int { return 11 } voce pode trocar por uma read-only computed property. var getTotalPlayers: Int { return 11 }. Depois é só se referir a ela sem os parênteses da função. – Leo Dabus Mar 12 '15 at 18:50
  • depois da uma olhada nessa resposta que voce vai entender. http://stackoverflow.com/a/28697136/2303865 – Leo Dabus Mar 12 '15 at 18:52
  • Entendi, pensei nesta possibilidade em deixar interface com **var TotalPlayers** por questões filosóficas queria que sempre fosse read-only e não soube como forçar isto através de interface, poderia utilizar herança. Obrigado pela ajuda e estou estudando mais afundo os conceitos de OOP. Descobri que achava que sabia algo, obrigado Leo. – ViTUu Mar 12 '15 at 18:56
0

I found a way to do this.. If you declare the typeOfSport in the player initialization function Sport.Type and then make the Sport initialization method required like so....

class Soccer : Sport {
override var players : Int { get { return 11 } }
}

class Basketball : Sport {
    override var players : Int { get { return 5 } }
}

class Sport {

    var teamName: String
    var players: Int { get { return 0 } }

    required init(teamName: String) {
        self.teamName = teamName
    }

}

class Player {
    let sport : Sport?

    init? (typeOfSport: Sport.Type, teamName: String) {
        self.sport = Soccer(teamName: teamName)
    }

    init? (typeOfSport: Basketball, teamName: String) {
        self.sport = Basketball(teamName: teamName)
    }
}

let me = Player(typeOfSport: Soccer.self, teamName: "chelsea")

let him = Player(typeOfSport: Basketball.self, teamName: "wizards")
Joshua Archer
  • 113
  • 3
  • 12