2

I have this class Minion

class GameObject : SKShapeNode {
    var health : Int!
}

class Character : GameObject {
    var cost : Int!
    var movementSpeed : Int!
    var damage : Int!
    var specials : [Special] = []
    required override init() {
        super.init()
    }
}

class Minion : Character {
    required init() {
        super.init()
        self.cost = 2
        self.movementSpeed = 21
        self.damage = 2
        self.path = SKShapeNode(rectOfSize: CGSizeMake(50, 50)).path
        self.fillColor = UIColor.redColor()
    }
}

If I want to instantiate the MetaType of this class I can do this.

let a = Minion.self()

with no error or crash.

However... If I try to do this.

let b = Minion.self
let ax = b() << NO ERROR BUT CRASHES

I receive a BAD_ACCESS_CODE crash. This crashes as well.

let b : Minion.Type = Minion.self
let ax = b()

Anyone have any ideas?

Airspeed Velocity
  • 40,491
  • 8
  • 113
  • 118
Michael Giba
  • 118
  • 4

1 Answers1

1

Looks to me like you’ve managed to convince the compiler to let you do something you shouldn’t.

If you removed your GameObject type from the hierarchy and just had Character inherit from SKShapeNode, you’d get a compiler error with your init:

error: 'required' initializer 'init(coder:)' must be provided by subclass of 'SKShapeNode'
}
^
SpriteKit.SKShapeNode:109:34: note: 'required' initializer is declared in superclass here
  @objc(initWithCoder:) required init?(coder aDecoder: NSCoder)

Yet somehow, by inserting GameObject into the middle there with no user-defined init method, it’ll let you get away with it at compile time – but not at runtime.

Looks like your problem is similar, though not necessarily the same, to this one here and the same solution may help.

Community
  • 1
  • 1
Airspeed Velocity
  • 40,491
  • 8
  • 113
  • 118