How to implement GKState's isValidNextState in Objective C

250 views Asked by At

It seems that all of the examples for GameplayKit are always in Swift. I've decided not to move over to swift for now and I've just been translating a lot of code into Objective C, which is fine most of the time.

I'm trying to implement the isValidNextState method from the GKState class, but I'm getting an error for the switch statement and I'm not sure what it wants...it seems in Swift this is fine, but not in obj C. The error that I'm getting is:

Error: Statement requires expression of integer type('__unsafe_unretained Class _Nonnull' invalid

What should I have in the switch statement instead of stateclass?

-(BOOL) isValidNextState:(Class)stateClass {
    switch (stateClass) { //ERROR ON THIS LINE
        case [InvulnerableState class]: //not sure what this should be either
            return YES;
            break;
        default:
            break;
    }
    return NO;
}

Here's the equivalent in Swift which works fine:

override func isValidNextState(stateClass: AnyClass) -> Bool {
    switch stateClass {
    case is InvulnerableState.Type:
        return true

    default:
        return false
    }
}
1

There are 1 answers

1
3d-indiana-jones On BEST ANSWER

Your isValidNextState method should be:

- (BOOL)isValidNextState:(Class)stateClass {
  return stateClass == [InvulnerableState class];
}

And if you have multiple next valid states, it should be, for example:

- (BOOL)isValidNextState:(Class)stateClass {
  return stateClass == [InvulnerableState class] ||
         stateClass == [InvulnerableState2 class];
}