---
title: "encodeConditionalObject(_:forKey:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nscoder/encodeconditionalobject(_:forkey:)"
---

# encodeConditionalObject(_:forKey:)

An encoding method for subclasses to override to conditionally encode an object, preserving common references to it, only if it has been unconditionally encoded.

## Declaration

```swift
func encodeConditionalObject(_ object: Any?, forKey key: String)
```

## Discussion

Discussion Subclasses must override this method if they support keyed coding. The encoded object is decoded with the decodeObject(forKey:) method. If objv was never encoded unconditionally, decodeObject(forKey:) returns nil in place of objv.

## See Also

### Encoding General Data

- [encodeArray(ofObjCType:count:at:)](foundation/nscoder/encodearray(ofobjctype:count:at:).md)
- [encode(_:forKey:)](foundation/nscoder/encode(_:forkey:)-7o6mu.md)
- [encodeBycopyObject(_:)](foundation/nscoder/encodebycopyobject(_:).md)
- [encodeByrefObject(_:)](foundation/nscoder/encodebyrefobject(_:).md)
- [encodeBytes(_:length:)](foundation/nscoder/encodebytes(_:length:).md)
- [encodeBytes(_:length:forKey:)](foundation/nscoder/encodebytes(_:length:forkey:).md)
- [encodeConditionalObject(_:)](foundation/nscoder/encodeconditionalobject(_:).md)
- [encode(_:)](foundation/nscoder/encode(_:)-1qd1e.md)
- [encode(_:forKey:)](foundation/nscoder/encode(_:forkey:)-9xiiu.md)
- [encode(_:forKey:)](foundation/nscoder/encode(_:forkey:)-84cez.md)
- [encodeCInt(_:forKey:)](foundation/nscoder/encodecint(_:forkey:).md)
- [encode(_:forKey:)](foundation/nscoder/encode(_:forkey:)-2dprz.md)
- [encode(_:forKey:)](foundation/nscoder/encode(_:forkey:)-5sk4z.md)
- [encode(_:forKey:)](foundation/nscoder/encode(_:forkey:)-dixg.md)
- [encode(_:)](foundation/nscoder/encode(_:)-9648d.md)
