Skip to content

Instantly share code, notes, and snippets.

@eduardo22i
Last active March 4, 2019 20:19
Show Gist options
  • Save eduardo22i/6ca76f8e044ba62eae7f0b356426abc5 to your computer and use it in GitHub Desktop.
Save eduardo22i/6ca76f8e044ba62eae7f0b356426abc5 to your computer and use it in GitHub Desktop.
Example of Enums in Swift using a Pokemon example.
import UIKit
// MARK: - Trainer
class Trainer {
/// Trainer's id
var id: String
/// Trainer's name
var name: String
init(id: String, name: String) {
self.id = id
self.name = name
}
}
extension Trainer: Equatable {
static func == (lhs: Trainer, rhs: Trainer) -> Bool {
return lhs.id == rhs.id
}
}
// MARK: - Pokemon
class Pokemon {
/// Pokedex number
let index: Int
/// Pokemon's name
var name: String
/// Pokemon's type
var type: Kind
/// Pokemon's status, be default a Pokemon status is Wild
var status: Status = .wild
/// Pokemon's trainer, returns nil is the Pokemon is wild
var trainer: Trainer? {
switch self.status {
case .wild:
return nil
case .owner(let trainer):
return trainer
}
}
init(index: Int, name: String, type: Kind) {
self.index = index
self.name = name
self.type = type
}
/// Evaluates the if the current Pokemon is good against a specific type
///
/// - Parameter type: the type that will be evaluated
/// - Returns: returns true if the Pokemons is strong against a type
func goodAgainst(type: Kind) -> Bool {
return self.type.strengths.contains(type)
}
}
// MARK: - Kind
extension Pokemon {
enum Kind: String, CaseIterable {
case electric, fire, water, grass
/// A string with the capitalized name
var name: String {
return self.rawValue.capitalized
}
/// A string with an emoji represeting the current type
var emoji: String {
switch self {
case .electric:
return "⚡️"
case .fire:
return "🔥"
case .water:
return "💧"
case .grass:
return "🌱"
}
}
/// An array of the types that are weak against the current type
var strengths: [Kind] {
switch self {
case .electric:
return [.fire, .water]
case .fire:
return [.grass]
case .water:
return [.fire]
case .grass:
return [.electric, .water]
}
}
/// Evaluates if the current Type is strong againts a Pokemon's Type
///
/// - Parameter pokemon: The Pokemon that will be evaluated
/// - Returns: returns true if the current type is strong againts the Pokemon, else returns false
func isStrongAgainstPokemon(_ pokemon: Pokemon) -> Bool {
return self.strengths.contains(pokemon.type)
}
}
}
// MARK: - Status
extension Pokemon {
enum Status {
case wild
case owner(trainer: Trainer)
/// Returns true is the status is wild
var isWild: Bool {
switch self {
case .wild:
return true
default:
return false
}
}
}
}
extension Pokemon.Status: Equatable {
static func == (lhs: Pokemon.Status, rhs: Pokemon.Status) -> Bool {
if lhs.isWild && rhs.isWild {
return true
}
let lhsTrainer: Trainer? = {
switch lhs {
case .wild:
return nil
case .owner(let trainer):
return trainer
}
}()
let rhsTrainer: Trainer? = {
switch rhs {
case .wild:
return nil
case .owner(let trainer):
return trainer
}
}()
return lhsTrainer == rhsTrainer
}
}
// Trainers
let eduardoTrainer = Trainer(id: "1", name: "Eduardo")
// Types
let electricTypeFromString = Pokemon.Kind.init(rawValue: "electric")
let electricType = Pokemon.Kind.electric
let waterType = Pokemon.Kind.water
// Pokemons
let pikachu = Pokemon(index: 25, name: "Pikachu", type: .electric)
let bulbasaur = Pokemon(index: 1, name: "Bulbasaur", type: .grass)
let charmander = Pokemon(index: 4, name: "Charmander", type: .fire)
let squirtle = Pokemon(index: 7, name: "Squirtle", type: .water)
// Iterating types
for type in Pokemon.Kind.allCases {
print(type)
}
if pikachu.type == .electric {
print("\(pikachu.name) is electric")//\(pikachu.type.rawValue)")
}
// Properties
if electricType.strengths.contains(waterType) {
print("\(electricType.emoji) type is good against \(waterType.emoji)")
}
// Methods
print(waterType.isStrongAgainstPokemon(pikachu))
if pikachu.goodAgainst(type: charmander.type) {
print("\(pikachu.name) beats \(charmander.name)")
}
if !pikachu.goodAgainst(type: bulbasaur.type) {
print("\(pikachu.name) is not good \(bulbasaur.name)")
}
// Associated values
charmander.status = .wild
pikachu.status = .owner(trainer: eduardoTrainer)
bulbasaur.status = .owner(trainer: eduardoTrainer)
if pikachu.status == .wild {
print("--")
}
if pikachu.status == bulbasaur.status {
print("Pikachu and Bulbasaur have the same status")
} else {
print("Pikachu and Bulbasaur don't ha d the same status")
}
if pikachu.status == charmander.status {
print("Pikachu and charmandar have the same status")
} else {
print("Pikachu and charmandar don't ha d the same status")
}
print(pikachu.trainer?.name ?? "No owner")
print(charmander.trainer?.name ?? "No owner")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment