【问题标题】:iOS / Swift - Unable to set Enum Value on Init()iOS / Swift - 无法在 Init() 上设置枚举值
【发布时间】:2015-07-26 23:06:59
【问题描述】:

我创建了 SKSpriteNode 类的扩展。我正在制作砖块,当它们被击中时会有不同的行为

import SpriteKit

class Brick: SKSpriteNode {

    enum type {
        case None, Green, Yellow, Orange, Red
    }

    static let colorMap = [
        Brick.type.Green : UIColor.greenColor(),
        Brick.type.Yellow : UIColor.yellowColor(),
        Brick.type.Orange : UIColor.orangeColor(),
        Brick.type.Red : UIColor.redColor()
    ]

    var brickType = Brick.type.None

    convenience init (size: CGSize, type: Brick.type) {
        self.init(color: UIColor.whiteColor(), size: size)

        // Here I set the initial type and color
        // The color is assigned just fine, but the brickType
        // variable is still Brick.type.None
        self.setType(type)
    }

    func gotHit () -> Int {
        switch (self.brickType) {
            case Brick.type.Yellow:
                setType(Brick.type.Green);
                break;

            case Brick.type.Orange:
                setType(Brick.type.Yellow);
                break;

            case Brick.type.Red:
                setType(Brick.type.Orange);
                break;

            case Brick.type.Green: // Green
                self.removeFromParent()
                return 1

            default:
                break
        }

        return 0
    }

    func setType (typeToSet: Brick.type) {
        self.brickType = typeToSet // only works when called from gotHit()
        self.color = Brick.colorMap[typeToSet]! // this works everytime
    }
}

然后我创建这个类的一个实例:

let brickPrototype = Brick(size: CGSizeMake(55, 25), type: Brick.type.Green)

我的问题是,尽管在convenience init () 中调用了setType(),公共brickType 变量的值仍然是默认值Brick.type.None。颜色更改没有问题,因此参数似乎正确传递。

如果我将默认的brickType变量设置为Brick.type.Yellow,并执行gotHit()函数,setType()函数将有效地将砖的类型更改为Brick.type.Green,再次调用后,通过调用self.removeFromParent() 从视图中删除节点。因此,我确信问题是当我从 convenience init() 调用函数时,即使我没有收到任何错误。

【问题讨论】:

  • 你能展示你是如何创建对象和检查类型的吗?我刚刚将您的代码粘贴到操场上,它工作正常
  • 在操场上仍然为我工作。
  • 我添加了一个简单的“将枚举转换为字符串函数” - gist.github.com/paulw11/2010f0474429a679a0b9 并返回适当的值
  • @Paulw11 我在init() 方法中设置它之后添加了一个println(self.brickType == Brick.type.None),它返回false(这是正确的)。但是我将它添加到gotHit() 函数中,它返回true。此外,就像您一样,它可以在操场上工作。但不在我的项目中。
  • 嗯。尝试直接在循环中实例化积木而不是复制。

标签: ios swift enums init


【解决方案1】:

如果您在初始化程序中第一次设置默认值,则无需设置默认值。尚未对此进行测试以查看它是否可以解决您的问题,但我确实清理了一些代码。

class Brick: SKSpriteNode {

enum BrickColorType: UInt {
    case Red
    case Orange
    case Yellow
    case Green //This order matters for nextColor

    func color() -> UIColor {
        switch self {
        case Green:
            return .greenColor() //Swift does not need break statements in cases.
        case Yellow:
            return .yellowColor()
        case Orange:
            return .orangeColor()
        case Red:
            return .redColor()
        }
    }

    func nextColor() -> BrickColorType? {
        return BrickColorType(rawValue: self.rawValue.successor()) //If self = green, this will return nil.
    }

}

var brickType: BrickColorType {
    didSet {
        self.color = brickType.color()
    }
}

init (size: CGSize, type: BrickColorType) {
    brickType = type
    super.init(texture: nil, color: .whiteColor(), size: size) //Because the earlier one was a convenience init and Xcode was complaining
    self.color = brickType.color() //Because didSet is not called in initializer
}

required init?(coder aDecoder: NSCoder) { //This is just boilerplate required to inherit from any NSObject
    brickType = .Red //Or whatever else. If you really want to add a None case to the enum, might I suggest instead making brickType optional?
    super.init(coder: aDecoder)
}

func gotHit () -> Int { //Consider making this a Bool

    if let next = self.brickType.nextColor() {
        //There is a valid next color
        brickType = next
        return 0
    }

    //There is no valid next color
    self.removeFromParent()
    return 1

}
} //Weird formatting because of StackOverflow

【讨论】:

    猜你喜欢
    • 2019-07-08
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多