<!--
{
  "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/GKRule",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "GameplayKit"
    ],
    "preciseIdentifier" : "c:objc(cs)GKRule"
  },
  "title" : "GKRule"
}
-->

# GKRule

A rule to be used in the context of a rule system, with a predicate to be tested and an action to be executed when the test succeeds.

```
class GKRule
```

## Overview

Evaluating a [`GKRuleSystem`](/documentation/GameplayKit/GKRuleSystem) object tests each of its rules, which typically examine the state or facts associated with the rule system, and executes the actions specified by each rule whose test passes, such as asserting or retracting facts in the rule system or modifying its state.

A rule has two parts: a predicate and an action.

- The rule’s *predicate* determines whether the rule has been satisfied, within the context of a given rule system. Evaluating a rule’s predicate typically involves examining information in the rule sytem’s [`state`](/documentation/GameplayKit/GKRuleSystem/state) dictionary or testing the membership grade of facts claimed by the system (see the [`facts`](/documentation/GameplayKit/GKRuleSystem/facts) property in [`GKRuleSystem`](/documentation/GameplayKit/GKRuleSystem) for details).
- The rule’s *action* is executed if and only if the rule’s predicate is satisfied. Rule actions typically involve asserting or retracting facts in the system (see the [`GKRuleSystem`](/documentation/GameplayKit/GKRuleSystem) methods listed in Asserting and Retracting Facts) or modifying information in the system’s [`state`](/documentation/GameplayKit/GKRuleSystem/state) dictionary.

There are multiple ways to create rules for use in a rule system, each with its own advantages.

- Typical rule predicates involve conditional logic tests on the properties of the containing rule system, and typical rule actions assert or retract facts. If your rules fit this pattern, you can use the [`init(predicate:assertingFact:grade:)`](/documentation/GameplayKit/GKRule/init(predicate:assertingFact:grade:)) and [`init(predicate:retractingFact:grade:)`](/documentation/GameplayKit/GKRule/init(predicate:retractingFact:grade:)) methods to create rules that are entirely data-driven—that is, they can be easily archived for later reuse, edited without compiling source code, and created at runtime.
- To create rules with entirely custom logic for both predicate and action, use the [`init(blockPredicate:action:)`](/documentation/GameplayKit/GKRule/init(blockPredicate:action:)) method. This method creates rules that are very flexible, but that cannot be archived for reuse.
- To create rules with more complex custom logic, implement your own rule classes: subclass [`GKRule`](/documentation/GameplayKit/GKRule) to build custom logic for both the rule’s predicate and its action, or subclass [`GKNSPredicateRule`](/documentation/GameplayKit/GKNSPredicateRule) to use an <doc://com.apple.documentation/documentation/Foundation/NSPredicate> object for the rule’s predicate and build custom logic only for the rule’s action. The reusability of custom rule classes depends on your implementation of such classes.

For more information about rules and rule systems, read [Rule Systems](https://developer.apple.com/library/archive/documentation/General/Conceptual/GameplayKit_Guide/RuleSystems.html#//apple_ref/doc/uid/TP40015172-CH10) in [GameplayKit Programming Guide](https://developer.apple.com/library/archive/documentation/General/Conceptual/GameplayKit_Guide/index.html#//apple_ref/doc/uid/TP40015172).

### Subclassing Notes

GameplayKit evaluates rules in the context of a [`GKRuleSystem`](/documentation/GameplayKit/GKRuleSystem) object, so custom rule classes should be *functional*—that is, they generally should not carry independent state that affects their predicate or action.

#### Methods to Override

Subclasses of [`GKRule`](/documentation/GameplayKit/GKRule) must implement both of the following methods:

- Override the [`evaluatePredicate(in:)`](/documentation/GameplayKit/GKRule/evaluatePredicate(in:)) method to evaluate your rule in the context of the provided rule system.
- Override the [`performAction(in:)`](/documentation/GameplayKit/GKRule/performAction(in:)) method to perform whatever actions should result when your rule is satisfied (that is, when your [`evaluatePredicate(in:)`](/documentation/GameplayKit/GKRule/evaluatePredicate(in:)) implementation returns <doc://com.apple.documentation/documentation/Swift/true>) in the context of the provided rule system.

#### Alternatives to Subclassing

- Use the [`init(predicate:assertingFact:grade:)`](/documentation/GameplayKit/GKRule/init(predicate:assertingFact:grade:)) or [`init(predicate:retractingFact:grade:)`](/documentation/GameplayKit/GKRule/init(predicate:retractingFact:grade:)) to create a rule that uses an <doc://com.apple.documentation/documentation/Foundation/NSPredicate> object for evaluation and whose action asserts or retracts a fact in the containing rule system.
- Subclass [`GKNSPredicateRule`](/documentation/GameplayKit/GKNSPredicateRule) instead to use an <doc://com.apple.documentation/documentation/Foundation/NSPredicate> object for evaluating the rule and write custom logic only for the rule’s action.
- Use the [`init(blockPredicate:action:)`](/documentation/GameplayKit/GKRule/init(blockPredicate:action:)) method to quickly create a rule whose custom logic is contained in block objects.

## Topics

### Creating Data-Driven Rules

[`+  ruleWithPredicate:assertingFact:grade:`](/documentation/GameplayKit/GKRule/init(predicate:assertingFact:grade:))

Creates a data-driven rule with the specified predicate, whose action asserts a fact in the rule system evaluating the rule.

[`+  ruleWithPredicate:retractingFact:grade:`](/documentation/GameplayKit/GKRule/init(predicate:retractingFact:grade:))

Creates a data-driven rule with the specified predicate, whose action retracts a fact in the rule system evaluating the rule.

### Creating Block-Based Rules

[`+  ruleWithBlockPredicate:action:`](/documentation/GameplayKit/GKRule/init(blockPredicate:action:))

Creates a rule whose predicate is evaluated and action is executed through the specified blocks.

### Setting the Order of Rules in a Rule System

[`salience`](/documentation/GameplayKit/GKRule/salience)

The importance of the rule relative to others in a rule system’s agenda.

### Evaluating a Rule

[`-  evaluatePredicateWithSystem:`](/documentation/GameplayKit/GKRule/evaluatePredicate(in:))

Returns a Boolean value indicating whether the rule has been satisfied in the context of the specified rule system.

[`-  performActionWithSystem:`](/documentation/GameplayKit/GKRule/performAction(in:))

Performs actions that should result when the rule is satisfied in the context of the specified rule system.

## Relationships

### Conforms To

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`CVarArg`](/documentation/Swift/CVarArg)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`Hashable`](/documentation/Swift/Hashable)

[`Equatable`](/documentation/Swift/Equatable)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

### Inherited By

[`GKNSPredicateRule`](/documentation/GameplayKit/GKNSPredicateRule)

---

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)