Laurence Wingo
Laurence Wingo

Reputation: 3952

Using an enum to set a property during initialization in swift

I'd like to learn how to set a unique gifted dance property during initialization using a breakdances enum that uses strings. I figured it would work but when I tried different variations of setting the property in init, I would get compile errors such as "assigning a property to itself" and many others. I've run out of ideas but I know its possible because some Cocoa classes do this during initialization such as UITableView when selecting preferred style.

import Foundation


enum Breakdances: String {
    case HoppityHop = "Hop Hop Hop and two step is what I do usually"
    case Greyjoy = "I made up this dance, pretty cool huh?"
    case ButtSwirl = "Let's do the butt swril"
    case PsychMovementWithHands = "Psych, Psych, Psych! Psych"
    case TheDab = "Dabbbbb!"
    case TheBump = "I'm doing the rump bump dance"
}




class Monkeys: Animals, canPlayAroundProtocol, randomJungleActivity {
    static var monkeysPopulation: Int = 0

    var uniqueGiftedDance: Breakdances

    override init(name:String){
        super.init(name: name)
        self.uniqueGiftedDance = uniqueGiftedDance
        Monkeys.monkeysPopulation += 1

    }

    override func makeSound() -> String {
        energyLevel -= 4

        return "Ah ah ah"
    }

    override func eatFood(){
        energyLevel += 2

    }

    override func sleep(){


    }

    func play(){

        let reducedEnergy = energyLevel - 8
        if reducedEnergy < 0 {
            print("\(name) is too tired, I don't have enough energy")
        }else {
        print("Oooo Oooo Oooo")
        print("\(name)'s energy level is now \(reducedEnergy)")
        }

    }

    func startUniqueJungleAct(){

        print(uniqueGiftedDance)

        print("Swinging from a tree and throwing banannas")
    }

    deinit {
        Monkeys.monkeysPopulation -= 1

    }

}

Here is my parent class:

import Foundation

protocol canPlayAroundProtocol {
    func play()
}

protocol randomJungleActivity {
    func startUniqueJungleAct()
}

class Animals {
    static var animalPopulation: Int = 0

    var name: String!

    var energyLevel: Int = 100


    init(name: String) {
        self.name = name

        Animals.animalPopulation += 1

        print("Another animal has given birth, animal population is now \(Animals.animalPopulation)")
    }

    func makeSound() -> String{
        energyLevel -= 3
        return "null"
    }

    func eatFood() {
        energyLevel += 5

    }

    func sleep(){
        energyLevel += 10

    }

    static func junglePerformSoundOff(){


    }

    func bathroomSound(){


    }

    deinit {
        Animals.animalPopulation -= 1
    }
}

Upvotes: 0

Views: 1273

Answers (1)

Alexander
Alexander

Reputation: 63399

You just need to add a new parameter to your initializer.

init(name: String, uniqueGiftedDance: Breakdances) {
    super.init(name: name)
    self.uniqueGiftedDance = uniqueGiftedDance
    Monkeys.monkeysPopulation += 1

}

Upvotes: 2

Related Questions