0

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
    }
}
7
  • 1
    where do you want uniqueGiftedDance to come from? Commented Jan 6, 2017 at 0:43
  • 1
    Your posted code has a lot of irrelevant material. People don't want to wade through all of that to get to the point. Please post a minimal, complete, and verifiable example. Commented Jan 6, 2017 at 0:45
  • @Alexander thanks and I'd like to set it when creating new instances of Monkey. So each monkey have have their own uniqueGiftedDance. I hope I answered your question. Commented Jan 6, 2017 at 0:45
  • 1
    So you'll want to give it as a parameter during initialization, correct? Commented Jan 6, 2017 at 0:46
  • 1
    So then... add it as a parameter to the initializer Commented Jan 6, 2017 at 0:47

1 Answer 1

2

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

}
Sign up to request clarification or add additional context in comments.

7 Comments

I tried this as well but get the error "Initializer does not override a designated initializer from its superclass".
So you'll have to get rid of the override keyword, to indicate that you no longer intend for this to be an override of an existing initializer form the superclass. I've updated my answer.
Removing override and adding "self.uniqueGiftedDance = uniqueGiftedDance" above super.init DID ITTT!!! Much much much obliged @Alexander !
@user6510422 Do you understand the point of the override keyword?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.