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

# encodeConditionalObject(_:)

Conditionally archives a given object.

## Declaration

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

## Parameters

- `object`: The object to archive.

## Discussion

Discussion This method overrides the superclass implementation to allow object to be encoded only if it is also encoded unconditionally by another object in the object graph. Conditional encoding lets you encode one part of a graph detached from the rest. (See Archives and Serializations Programming Guide for more information.) This method should be invoked only from within an encode(with:) method. If object is nil, the NSArchiver object encodes it unconditionally as nil. This method raises an NSInvalidArgumentException if no root object has been encoded.

## See Also

### Archiving data

- [archivedData(withRootObject:)](foundation/nsarchiver/archiveddata(withrootobject:).md)
- [archiveRootObject(_:toFile:)](foundation/nsarchiver/archiverootobject(_:tofile:).md)
- [encodeRootObject(_:)](foundation/nsarchiver/encoderootobject(_:).md)
