---
title: "encode(_:forKey:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nscoder/encode(_:forkey:)-10qhm"
---

# encode(_:forKey:)

Encodes a rectangle and associates it with the specified key in the receiver’s archive.

## Declaration

```swift
func encode(_ rect: CGRect, forKey key: String)
```

## Parameters

- `rect`: The rectangle to encode.
- `key`: The key identifying the data.

## Discussion

Discussion When decoding the data from the archive, you pass the value in the key parameter to the corresponding decodeCGRect(forKey:) method to retrieve the data.

## See Also

### Related Documentation

- [decodeCGRect(forKey:)](foundation/nscoder/decodecgrect(forkey:).md)

### Encoding Geometry-Based Data

- [encode(_:forKey:)](foundation/nscoder/encode(_:forkey:)-29jyx.md)
- [encode(_:forKey:)](foundation/nscoder/encode(_:forkey:)-7z9kc.md)
- [encode(_:forKey:)](foundation/nscoder/encode(_:forkey:)-6wq3n.md)
- [encode(_:forKey:)](foundation/nscoder/encode(_:forkey:)-26fxa.md)
- [encode(_:forKey:)](foundation/nscoder/encode(_:forkey:)-7oo2n.md)
- [encode(_:forKey:)](foundation/nscoder/encode(_:forkey:)-44zsc.md)
- [encode(_:forKey:)](foundation/nscoder/encode(_:forkey:)-9d1qy.md)
