在swift中初始化期间使用枚举设置属性

在swift中初始化期间使用枚举设置属性,swift,enums,Swift,Enums,我想学习如何使用使用字符串的breakdances枚举在初始化期间设置一个独特的gifted dance属性。我想它会起作用,但当我尝试在init中设置属性的不同变体时,我会遇到编译错误,例如“将属性分配给自身”和许多其他错误。我已经没有想法了,但我知道这是可能的,因为有些Cocoa类在初始化时会这样做,比如在选择首选样式时使用UITableView import Foundation enum Breakdances: String { case HoppityHop = "Hop

我想学习如何使用使用字符串的breakdances枚举在初始化期间设置一个独特的gifted dance属性。我想它会起作用,但当我尝试在init中设置属性的不同变体时,我会遇到编译错误,例如“将属性分配给自身”和许多其他错误。我已经没有想法了,但我知道这是可能的,因为有些Cocoa类在初始化时会这样做,比如在选择首选样式时使用UITableView

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

    }

}

您只需要向初始值设定项添加一个新参数

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

}

您只需要向初始值设定项添加一个新参数

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

}

你想从哪里来
uniqueGiftedDance
?你发布的代码有很多不相关的内容。人们不想为了达到目的而涉过所有这些。请发表一篇文章。@Alexander谢谢,我想在创建新的Monkey实例时设置它。所以每只猴子都有自己独特的舞蹈。我希望我回答了你的问题。所以你会想在初始化时把它作为一个参数,对吗?那么。。。将其作为参数添加到初始值设定项中您希望
uniqueGiftedDance
来自何处?您发布的代码有很多不相关的内容。人们不想为了达到目的而涉过所有这些。请发表一篇文章。@Alexander谢谢,我想在创建新的Monkey实例时设置它。所以每只猴子都有自己独特的舞蹈。我希望我回答了你的问题。所以你会想在初始化时把它作为一个参数,对吗?那么。。。将其作为参数添加到初始值设定项我也尝试过这样做,但出现错误“初始值设定项不会覆盖其超类中指定的初始值设定项”。因此,您必须去掉
override
关键字,以表明您不再打算将其作为超类中现有初始值设定项的
override
。我已经更新了我的答案。删除覆盖并在super.init上添加“self.uniqueGiftedDance=uniqueGiftedDance”!!!非常感谢@Alexander@user6510422您了解
覆盖
关键字的意义吗?让我们来看看。我也尝试过这个,但得到错误“Initializer不会覆盖其超类中指定的初始值设定项”。因此您必须去掉
覆盖
关键字,表示您不再打算将其作为超类中现有初始值设定项的重写。我已经更新了我的答案。删除覆盖并在super.init上添加“self.uniqueGiftedDance=uniqueGiftedDance”!!!非常感谢@Alexander@user6510422您了解
覆盖
关键字的意义吗?让我们来看看。