我相信这对您来说是一个简单的问题。
如何用一个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")
谢谢您的回复
最佳答案
如果要将参数作为类,请使用Class.Type
或AnyClass
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,带有两个参数的快速功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35141591/