【问题标题】:Gameplaykit GKState, swift func with two parametersGameplaykit GKState,带有两个参数的快速函数
【发布时间】:2016-02-01 22:25:32
【问题描述】:

我相信这对你来说是一个简单的问题。

如何用一个 GKState 编写具有两个参数的 func?

更新

苹果使用 func willExitWithNextState(_ nextState: GKState)

如果我使用somefunc(state:GKState) 工作很好

虽然somefunc(state:GKState, string:String) 不起作用,为什么???

其他示例

我试过这个:

class Pippo:GKState {}

//1
func printState (state: GKState?) {
    print(state)
}

printState(Pippo) //Error cannot convert value of type '(Pippo).Type' (aka 'Pippo.Type') to expected argument type 'GKState?'

//2
func printStateAny (state: AnyClass?) {
    print(state)
}
printStateAny(Pippo) //NO Error


//3
func printStateGeneral <T>(state: T?) {
    print(state)
}
printStateGeneral(Pippo) //No Error

//4
func printStateAnyAndString (state: AnyClass?, string:String) {
    print(state)
    print(string)
}

printStateAnyAndString(Pippo/*ExpectedName Or costructor*/, string: "Hello") //ERROR
printStateAnyAndString(Pippo()/*ExpectedName Or costructor*/, string: "Hello") //ERROR cannot convert value of type 'Pippo' to expected argument type 'AnyClass?'

解决方案感谢@0x141E

func printStateAnyAndString (state: GKState.Type, string:String) {
    switch state {
    case is Pippo.Type:
        print("pippo")
    default:
        print(string)
    }
}

printStateAnyAndString(Pippo.self, string: "Not Pippo")

感谢回复

【问题讨论】:

    标签: ios swift types gameplay-kit


    【解决方案1】:

    如果您希望参数成为类,请使用Class.TypeAnyClass

    func printState (state: AnyClass, string:String) {
        print(state)
        print(string)
    }
    

    并使用Class.self 作为参数

    printState(Pippo.self, string:"hello pippo")
    

    更新

    如果你的函数定义是

    func printState (state:GKState, string:String) {
        if state.isValidNextState(state.dynamicType) {
            print("\(state.dynamicType) is valid")
        }
        print(state)
        print(string)
    }
    

    您需要传入GKState(或GKState 的子类)的实例作为第一个参数,而不是类/子类本身。例如,

    let pippo = Pippo()
    
    printState (pippo, "Hello")
    

    【讨论】:

    • 谢谢,我会在家里试试。但是为什么苹果使用 func willExitWithNextState(_ nextState: GKState) 呢?为什么如果我使用 somefunc(state:GKState) 有效,而 somefunc(state:GKState, string:String) 无效???
    • 第一个解决方案就是我想要的!谢谢!不是第二个,因为我必须检查班级,而不是距离
    • 如果您需要检查对象的类型/类,您可以使用.dynamicType(请参阅我更新的第二个解决方案)。
    【解决方案2】:

    在整个示例代码中,您都使用了 AnyClass,而您应该(可能)使用 AnyObject。 AnyClass 指的是类定义,而 AnyObject 是类的实例。

    class MyClass { }
    
    func myFunc1(class: AnyClass)
    func myFunc2(object: AnyObject)
    
    let myObject = MyClass() // create instance of class
    
    myFunc1(MyClass)         // myFunc1 is called with a class
    myFunc2(myObject)        // myFunc2 is called with an instance
    

    您还使用“?”将大部分参数设为可选,而它看起来不是必需的。例如:

    printState(nil) // What should this do?
    

    【讨论】:

    • 我需要类定义。我的代码需要可选参数。我会更新一些代码来更好地解释
    猜你喜欢
    • 2021-12-14
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多