<!--
{
  "availability" : [
    "iOS: 9.0.0 -",
    "iPadOS: 9.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.11.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "GameplayKit",
  "identifier" : "/documentation/GameplayKit/GKStateMachine/enter(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "GameplayKit"
    ],
    "preciseIdentifier" : "c:objc(cs)GKStateMachine(im)enterState:"
  },
  "title" : "enter(_:)"
}
-->

# enter(_:)

Attempts to transition the state machine from its current state to a state of the specified class.

```
func enter(_ stateClass: AnyClass) -> Bool
```

## Parameters

`stateClass`

The class of state into which to attempt a transition.

## Return Value

<doc://com.apple.documentation/documentation/Swift/true> if the transition was successful; otherwise <doc://com.apple.documentation/documentation/Swift/false>.

## Discussion

You build a state machine by creating a unique [`GKState`](/documentation/GameplayKit/GKState) subclass for each distinct state possible for the machine. The [`isValidNextState(_:)`](/documentation/GameplayKit/GKState/isValidNextState(_:)) method of each state object determines which other states a state machine is allowed to transition into from that state. Calling this method first tests whether a transition from the current state to the specified state is valid; if not, this method returns <doc://com.apple.documentation/documentation/Swift/false>.

If a transition is allowed, the state machine sends the [`willExit(to:)`](/documentation/GameplayKit/GKState/willExit(to:)) message to its current state object. Then, the new state object replaces the value of the [`currentState`](/documentation/GameplayKit/GKStateMachine/currentState) property. Finally, the state machine sends the  [`didEnter(from:)`](/documentation/GameplayKit/GKState/didEnter(from:)) message to the new current state object, and this method returns <doc://com.apple.documentation/documentation/Swift/true>.

A newly created state machine’s [`currentState`](/documentation/GameplayKit/GKStateMachine/currentState) property is `nil`—to choose and enter an initial state, use the [`enter(_:)`](/documentation/GameplayKit/GKStateMachine/enter(_:)) method. In this case, the [`enter(_:)`](/documentation/GameplayKit/GKStateMachine/enter(_:)) call always succeeds.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)