---
title: "encodeConditional(_:forKey:)"
framework: swift
role: symbol
role_heading: Instance Method
path: "swift/keyedencodingcontainerprotocol/encodeconditional(_:forkey:)"
---

# encodeConditional(_:forKey:)

Encodes a reference to the given object only if it is encoded unconditionally elsewhere in the payload (previously, or in the future).

## Declaration

```swift
mutating func encodeConditional<T>(_ object: T, forKey key: Self.Key) throws where T : AnyObject, T : Encodable
```

## Parameters

- `object`: The object to encode.
- `key`: The key to associate the object with.

## Discussion

Discussion For encoders which don’t support this feature, the default implementation encodes the given object unconditionally. note: EncodingError.invalidValue if the given value is invalid in the current context for this format.
