gpt4 book ai didi

ios - Gameplaykit GKState,带有两个参数的 swift func

转载 作者:行者123 更新时间:2023-11-30 13:44:29 24 4
gpt4 key购买 nike

我确信这对您来说是一个简单的问题。

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

更新

苹果使用 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")

谢谢回复

最佳答案

如果您希望参数是类,请使用 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")

关于ios - Gameplaykit GKState,带有两个参数的 swift func,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35141591/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com