Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图在swift中初始化超类属性,但遇到错误_Swift - Fatal编程技术网

试图在swift中初始化超类属性,但遇到错误

试图在swift中初始化超类属性,但遇到错误,swift,Swift,我是swift的新手,目前正在学习初始化者。我遇到了重写父类初始值设定项的问题。 我遇到一个编译器错误“在super init初始化self之前在属性访问“ImageTexture”中使用“self” 我想一个新的形象,以得到使用时,特别徽章类是调用 这是我得到错误的特殊徽章类 class SpecialBadge: Badge { enum BadgeAnimation: Int { case GrowAndShrink = 0, Rotate, Shake

我是swift的新手,目前正在学习初始化者。我遇到了重写父类初始值设定项的问题。 我遇到一个编译器错误“在super init初始化self之前在属性访问“ImageTexture”中使用“self”

我想一个新的形象,以得到使用时,特别徽章类是调用

这是我得到错误的特殊徽章类

class SpecialBadge: Badge {

    enum BadgeAnimation: Int {
        case GrowAndShrink = 0, Rotate, Shake
    }

    override init(requestType: UDRequestType) {

        let animation = BadgeAnimation(rawValue: Int(arc4random_uniform(3)))
        self.imageTexture = SKTexture(imageNamed: "BadgeTeal")
        super.init(requestType: requestType)


        switch animation! {
        case .GrowAndShrink:
            growShrink()

        case .Rotate:
            rotate()

        case .Shake:
            shake()
        }
    }
}
我在线呼叫时出错:

self.imageTexture = SKTexture(imageNamed: "BadgeTeal")
调用
super.init(requestType:requestType)

我的家长班

class Badge: SKSpriteNode {

    var requestType: UDRequestType
    var imageTexture: SKTexture?

    init(requestType: UDRequestType) {
        self.requestType = requestType

        if let texture = imageTexture {
            super.init(texture: texture, color: UIColor.clearColor(), size: CGSizeMake(48, 48))
        } else {
            let newTexture = SKTexture(imageNamed: "BadgeMagenta")
            super.init(texture: newTexture, color: UIColor.clearColor(), size: CGSizeMake(48, 48))
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

我不知道如何初始化imageTexture,以便在调用SpecialBadge中的初始值设定项之前可以使用不同的图像,任何帮助都将不胜感激

这只是操作顺序的问题。这是非法的:

self.imageTexture = SKTexture(imageNamed: "BadgeTeal")
super.init(requestType: requestType)
这是合法的:

super.init(requestType: requestType)
self.imageTexture = SKTexture(imageNamed: "BadgeTeal")

规则是,在对自己的类执行任何特殊操作之前,必须完成超类的初始化。

这只是操作顺序的问题。这是非法的:

self.imageTexture = SKTexture(imageNamed: "BadgeTeal")
super.init(requestType: requestType)
这是合法的:

super.init(requestType: requestType)
self.imageTexture = SKTexture(imageNamed: "BadgeTeal")

规则是,在对自己的类执行任何特殊操作之前,必须完成超类的初始化。

如果让
if
绑定将始终失败,因为
imageTexture
没有默认值。这是故意的吗?
if let
绑定总是会失败,因为
imageTexture
没有默认值。这是故意的吗?嘿,我尝试过这种方法,但我的目标是使用SKTexture(ImageName:“BadgeTeal”),但是当初始化器被调用时,imageTexture是空的,它使用Badge类中的SKTexture(ImageName:“BadgeMagenta”)。有没有办法覆盖Badge类初始值设定项中使用的super.init?我不知道问题出在哪里。只需先调用
super.init
dead,然后再执行其他操作。嘿,我尝试过这种方法,但我的目标是使用SKTexture(ImageName:“BadgeTeal”),但当初始化器被调用时,imageTexture为空,并且它使用Badge类中的SKTexture(ImageName:“BadgeMagenta”)。有没有办法覆盖Badge类初始值设定项中使用的super.init?我不知道问题出在哪里。只需先调用super.initdead,然后再做其他事情。