0

I'm sure that is a simple question for you.

How can I write a func with two parameters with one GKState?

UPDATE

Apple use func willExitWithNextState(_ nextState: GKState)

If I use somefunc(state:GKState) works fine

while somefunc(state:GKState, string:String) does't work, why???

Other example

I've tried this:

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?'

SOLUTION THANKS @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")

Thanks for reply

Simone Pistecchia
  • 2,746
  • 3
  • 18
  • 30

2 Answers2

0

Throughout your sample code you have used AnyClass, whereas you should (probably) be using AnyObject. AnyClass refers to a class definition, whereas AnyObject is an instance of a class.

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

You have also made most of your parameters Optionals with the "?", whereas it doesn't look required. For example:

printState(nil) // What should this do?
Michael
  • 8,891
  • 3
  • 29
  • 42
0

If you want a parameter to be a class, use Class.Type or AnyClass

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

and use Class.self as the argument

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

Update

If your function definition is

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

you'll need to pass in an instance of GKState (or a subclass of GKState) as the first argument, not the class/subclass itself. For example,

let pippo = Pippo()

printState (pippo, "Hello")
0x141E
  • 12,613
  • 2
  • 41
  • 54
  • Thanks I'll try at home. But why apple use func willExitWithNextState(_ nextState: GKState) ? And why if I use somefunc(state:GKState) works, while somefunc(state:GKState, string:String) does't work??? – Simone Pistecchia Feb 03 '16 at 08:38
  • The first solution is what i want! thanks! Not the second because i must check the class, not the istance – Simone Pistecchia Feb 03 '16 at 17:56
  • You can use `.dynamicType` if you need to check the type/class of an object (see my updated 2nd solution). – 0x141E Feb 03 '16 at 18:40