<!--
{
  "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/GKNSPredicateRule/init(predicate:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "GameplayKit"
    ],
    "preciseIdentifier" : "c:objc(cs)GKNSPredicateRule(im)initWithPredicate:"
  },
  "title" : "init(predicate:)"
}
-->

# init(predicate:)

Initializes a rule with the specified predicate.

```
init(predicate: NSPredicate)
```

## Parameters

`predicate`

A predicate to be tested when evaluating the rule.

## Return Value

A new predicate-based rule object.

## Discussion

Rules based on <doc://com.apple.documentation/documentation/Foundation/NSPredicate> objects typically test information in the [`state`](/documentation/GameplayKit/GKRuleSystem/state) dictionary of the rule system evaluating the rule. For example, the following code creates a rule you might use to determine whether an enemy character in a game behaves aggressively.

```objc
// MyNSPredicateRule is a GKNSPredicateRule subclass
NSPredicate *healthTest = [NSPredicate predicateWithFormat:@"$player.health > 50"];
MyNSPredicateRule *rule = [MyNSPredicateRule alloc] initWithPredicate:healthTest];
```

This example presumes the rule system’s state dictionary contains an object for the key `player`, which in turn exposes a numeric value for the key `health`. The [`GKNSPredicateRule`](/documentation/GameplayKit/GKNSPredicateRule) class by itself does nothing in its [`performAction(in:)`](/documentation/GameplayKit/GKRule/performAction(in:)) method—to create actions for predicate-based rules, you must subclass [`GKNSPredicateRule`](/documentation/GameplayKit/GKNSPredicateRule).

For more information, see [GameplayKit Programming Guide](https://developer.apple.com/library/archive/documentation/General/Conceptual/GameplayKit_Guide/index.html#//apple_ref/doc/uid/TP40015172).

---

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)