简体   繁体   English

如何在Swift中使用枚举内的函数选择枚举值?

[英]How to choose an enum value with a function inside the enum in Swift?

I'm working to create a SpriteKit game and I'm trying to choose an enum value with a function inside my enum so that I can use each value outside of it. 我正在创建一个SpriteKit游戏,我试图选择一个枚举值,该枚举值带有一个位于枚举内部的函数,以便可以使用其外部的每个值。

This is my code: 这是我的代码:

enum Classification: Int, CustomStringConvertible {
    case Circle = 0, Square, Triangle

    var classificationName: String {
        switch self {
        case .Circle:
            return "circle"
        case .Square:
            return "square"
        case .Triangle:
            return "triangle"
        }
    }

    var description: String {
        return self.classificationName
    }

    var classificationSpriteName: String {
        let classificationSpriteName = [
        "circle",
        "square",
        "triangle"
        ]
        return classificationSpriteName[rawValue]
    }

    static func useGeometry() -> Classification {
        return Classification(rawValue: Int())!
    }
}

class Shape: CustomStringConvertible, Hashable {
    var hashValue: Int {
        return self.column ^ self.row
    }
    var column: Int
    var row: Int
    let shapeType: Classification

    var shapeSprite: SKSpriteNode?

    var classificationName: String {
        return shapeType.classificationName
    }

    ….(code)…

    final class func chosenShape(shapeStartingColumn:Int, shapeStartingRow:Int) -> Shape {
        switch Classification.useGeometry() {
        case .Circle:
            return CircleShape(column:shapeStartingColumn, row:shapeStartingRow, shapeType: Classification(rawValue: 0)!)
        case .Square:
            return SquareShape(column:shapeStartingColumn, row:shapeStartingRow, shapeType: Classification(rawValue: 1)!)
        case .Triangle:
            return SquareShape(column:shapeStartingColumn, row:shapeStartingRow, shapeType: Classification(rawValue: 2)!)

        }
    }

I can't choose anything but the circle. 除了圆圈,我别无选择。

Any ideas on how to solve that? 关于如何解决的任何想法?

The function useGeometry returns .Circle every time because you start it with rawValue 0, which is the result of Int() . 每次使用useGeometry函数useGeometry返回.Circle因为您从rawValue 0开始,这是Int()的结果。

Implement that funcion to return self (shoud not be static anymore) and use shapeType in the switch. 实现该功能以返回self (应该不再是静态的)并在开关中使用shapeType

The problem is this function: 问题是这个功能:

static func useGeometry() -> Classification {
    return Classification(rawValue: Int())!
}

You are creating a new Classification with a rawValue evaluated by Int(), which is going to return a new Int object with a default value (Int() == 0). 您正在创建一个带有由Int()评估的rawValue的新分类,它将返回一个默认值(Int()== 0)的新Int对象。

So when you call useGeometry from your switch statement, it's effectively always going to return .Circle, because the rawValue is effectively always 0. 因此,当您从switch语句调用useGeometry时,实际上总是返回.Circle,因为rawValue实际上始终为0。

You probably want to pass in an integer, eg maybe the row value?, to useGeometry, something which matches up to a Classification enum value. 您可能希望传入一个整数(例如行值?)来使用Geometry,它可以与Category枚举值匹配。 Something like this: 像这样:

static func useGeometry(value: Int) -> Classification {
    return Classification(rawValue: value)!
}

Note: Be careful that you don't call it with an invalid value or you will crash, because you're force unwrapping the result of the constructor. 注意:请注意不要用无效值调用它,否则将导致崩溃,因为您将强制拆开构造函数的结果。 And then call it with a value: 然后用一个值调用它:

switch Classification.useGeometry(value: shapeStartingRow) {
...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM