Contents

createBranch(predicate:attribute:)

Creates a child node that the decision tree should use when the current node’s attribute satisfies the specified predicate.

Declaration

func createBranch(predicate: NSPredicate, attribute: any NSObjectProtocol) -> Self

Parameters

  • predicate:

    The predicate against which to test this node’s attribute.

  • attribute:

    The attribute for the child node to create.

Return Value

The newly created child node.

Discussion

Use this method to specify a possible result of testing this node’s attribute (that is, an answer for this node’s question). The attribute parameter an have either of two kinds of values: if this test produces the decision tree’s final result (or action), the value identifies that action; if the next step in the decision tree is to be another question, the value identifies the next attribute to test (or question to ask).

The predicate parameter is a predicate for testing values corresponding to this node’s attribute, so this method is useful for questions whose answers involve passing a threshold value, matching a substring, matching an element in a set, or some more complex test. (If a question has a few specific answer values, see the createBranch(value:attribute:) method instead.) For example, a strategy combat game might use this method to choose an attack based on the opponent’s health level:

See Also

Creating Child Nodes for Decision Branches