Je suis sûr que c'est une question simple pour vous.Gameplaykit GKState, fonction rapide avec deux paramètres
Comment écrire une fonction avec deux paramètres avec un GKState?
MISE À JOUR
utiliser d'Apple func willExitWithNextState(_ nextState: GKState)
Si je somefunc(state:GKState)
œuvres fines
tandis que somefunc(state:GKState, string:String)
travail does't, pourquoi ???
Autre exemple
J'ai essayé ceci:
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?'
MERCI SOLUTION @ 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")
Merci pour la réponse
Merci, je vais essayer à la maison. Mais pourquoi apple utilise func willExitWithNextState (_ nextState: GKState)? Et pourquoi si j'utilise somefunc (state: GKState) fonctionne, alors que somefunc (state: GKState, string: String) ne fonctionne pas ??? –
La première solution est ce que je veux! Merci! Pas la seconde parce que je dois vérifier la classe, pas l'istance –
Vous pouvez utiliser '.dynamicType' si vous avez besoin de vérifier le type/la classe d'un objet (voir ma 2ème solution mise à jour). – 0x141E