<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/OutputStream/toMemory()",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSOutputStream(cm)outputStreamToMemory"
  },
  "title" : "toMemory()"
}
-->

# toMemory()

Creates and returns an initialized output stream that will write stream data to memory.

```
class func toMemory() -> Self
```

## Return Value

An initialized output stream that will write stream data to memory.

## Discussion

The stream must be opened before it can be used.

You retrieve the contents of the memory stream by sending the message [`property(forKey:)`](/documentation/Foundation/Stream/property(forKey:)) to the receiver with an argument of `NSStreamDataWrittenToMemoryStreamKey`.

## See Also

[`outputStreamToBuffer:capacity:`](/documentation/Foundation/NSOutputStream/outputStreamToBuffer:capacity:)

Creates and returns an initialized output stream that can write to a provided buffer.

  [Stream Programming Guide](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Streams/Streams.html#//apple_ref/doc/uid/10000188i)

[`outputStreamToFileAtPath:append:`](/documentation/Foundation/NSOutputStream/outputStreamToFileAtPath:append:)

Creates and returns an initialized output stream for writing to a specified file.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)