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

# encodeConditionalObject(_:)

An encoding method for subclasses to override to conditionally encode an object, preserving common references to it.

## Declaration

```swift
func encodeConditionalObject(_ object: Any?)
```

## Discussion

Discussion In the overriding method, object should be encoded only if it’s unconditionally encoded elsewhere (with any other encode...Object: method). This method must be matched by a subsequent decodeObject() message. Upon decoding, if object was never encoded unconditionally, decodeObject returns nil in place of object. However, if object was encoded unconditionally, all references to object must be resolved. NSCoder’s implementation simply invokes encode(_:).

## See Also

### Related Documentation

- [encodeConditionalObject(_:)](foundation/nsarchiver/encodeconditionalobject(_:).md)

### 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(_:forKey:)](foundation/nscoder/encodeconditionalobject(_:forkey:).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)
