I am still a beginner at Swift. I have a class NonPlayerCharacter as a parent class and a subclass Goblin that inherits from it. I defined health and power inside the NonPlayer class, and I defined weapon inside the Goblin. Then I declare a new variable so I can call class Goblin and change the values of health, power, and weapon but I can't see weapon inside the bracket (I only see health and power). I tried to make an init function, but I got this error " Super.init isn't called on all paths before returning from initializer ". I explained my problem more clearly in the comments below inside my code.
I have this class on the Playground
class NonPlayerCharacter
{
var health: Int
var power: Int
init() {
health = 0
power = 0
}
init(health: Int , power : Int) {
self.health = health
self.power = power
}
func attack () -> String
{
return "attack from NonPlayer Character"
} }
var NonPlayerMethod = NonPlayerCharacter(health: 100, power: 90)
//and this is the SubClass:
class Goblin: NonPlayerCharacter
{
var weapon : Int = 0
override func attack() -> String {
return "attack from Goblin"
}
}
var GoblinMethod = Goblin(health: 40, power: 12)
GoblinMethod.weapon = 10
GoblinMethod.attack()
//I tried to make initialization like this in the SubClass**
class Goblin: NonPlayerCharacter
{
var weapon : Int = 0
Init ( weapon: Int )
{
self.weapon = weapon
}
}
// so I can change the values like this :
var GoblinMethod = Goblin( weapon: 30 , health: 20, power: 50)
// I got this error ( Super.init isn't called on all paths before returning from initializer )
//I don't think I need to override Init as the weapon only in the SubClass.
it didn't workmean? - AlexanderGoblinrequires the initialization of aNonPlayerCharacter, so you'll need to callsuper.init(...)at some point in your initializer - Alexander