---
title: "compressed(using:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nsdata/compressed(using:)"
---

# compressed(using:)

Returns a new data object by compressing the data object’s bytes.

## Declaration

```swift
func compressed(using algorithm: NSData.CompressionAlgorithm) throws -> Self
```

## Parameters

- `algorithm`: An algorithm used to compress the data. For a list of available algorithms, see doc://com.apple.foundation/documentation/Foundation/NSData/CompressionAlgorithm.

## Return Value

Return Value An NSData instance that contains the compressed buffer data.

## Discussion

Discussion Use this method to compress in-memory data when you want to reduce memory usage and can afford the time to compress and decompress it. If your data object is already in a compressed format, such as media formats like JPEG images or AAC audio, additional compression may provide minimal or no reduction in memory usage. To restore this data, use decompressed(using:), and specify the algorithm originally used to compress the data. The following example shows how to compress the data from a string and prints the sizes of the data instances to illustrate the amount of compression: var string = "NSData and its mutable subclass NSMutableData provide data objects, or object-oriented wrappers for byte buffers. Data objects let simple allocated buffers (that is, data with no embedded pointers) take on the behavior of Foundation objects." let data = Data(string.utf8) as NSData print ("original data size: \(data.count) bytes") do {     let compressedData = try data.compressed(using: .zlib)     print("zlib compressed size: \(compressedData.count) bytes") } catch {     print ("Compression error: \(error)") } // Prints: //  original data size: 241 bytes //  zlib compressed size: 158 bytes

## See Also

### Compressing and Decompressing Data

- [decompressed(using:)](foundation/nsdata/decompressed(using:).md)
- [NSData.CompressionAlgorithm](foundation/nsdata/compressionalgorithm.md)
- [NSCompressionErrorMaximum](foundation/nscompressionerrormaximum-swift.var.md)
- [NSCompressionErrorMinimum](foundation/nscompressionerrorminimum-swift.var.md)
- [NSCompressionFailedError](foundation/nscompressionfailederror-swift.var.md)
- [NSDecompressionFailedError](foundation/nsdecompressionfailederror-swift.var.md)
