<!--
{
  "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/Stream",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSStream"
  },
  "title" : "Stream"
}
-->

# Stream

An abstract class representing a stream.

```
class Stream
```

## Overview

This class’s interface is common to all Cocoa stream classes, including its concrete subclasses [`InputStream`](/documentation/Foundation/InputStream) and [`OutputStream`](/documentation/Foundation/OutputStream).

[`Stream`](/documentation/Foundation/Stream) objects provide an easy way to read and write data to and from a variety of media in a device-independent way. You can create stream objects for data located in memory, in a file, or on a network (using sockets), and you can use stream objects without loading all of the data into memory at once.

By default, [`Stream`](/documentation/Foundation/Stream) instances that aren’t file-based are non-seekable, one-way streams (although custom seekable subclasses are possible). After you provide or consume data, you can’t retrieve the data from the stream.

### Subclassing Notes

[`Stream`](/documentation/Foundation/Stream) is an abstract class, incapable of instantiation and intended for you to subclass it. It publishes a programmatic interface that all subclasses must adopt and provide implementations for. The two Apple-provided concrete subclasses of [`Stream`](/documentation/Foundation/Stream), [`InputStream`](/documentation/Foundation/InputStream) and [`OutputStream`](/documentation/Foundation/OutputStream), are suitable for most purposes. However, there might be situations when you want a peer subclass to [`InputStream`](/documentation/Foundation/InputStream) and [`OutputStream`](/documentation/Foundation/OutputStream). For example, you might want a class that implements a full-duplex (two-way) stream, or a class whose instances are capable of seeking through a stream.

#### Methods to Override

All subclasses must fully implement the following methods:

- ``doc://com.apple.foundation/documentation/Foundation/Stream/open()`` and ``doc://com.apple.foundation/documentation/Foundation/Stream/close()``

Implement [`open()`](/documentation/Foundation/Stream/open()) to open the stream for reading or writing and make the stream available to the client directly or, if the stream object is scheduled on a run loop, to the delegate. Implement [`close()`](/documentation/Foundation/Stream/close()) to close the stream and remove the stream object from the run loop, if necessary. A closed stream should still be able to accept new properties and report its current properties. Once you close a stream, you can’t reopen it.

- ``doc://com.apple.foundation/documentation/Foundation/Stream/delegate``

Return and set the delegate. By a default, a stream object must be its own delegate; so a [`delegate`](/documentation/Foundation/Stream/delegate) message with an argument of `nil` should restore this delegate. Don’t retain the delegate to prevent retain cycles.

To learn about delegates and delegation, read “Delegation” in Cocoa Fundamentals Guide.

- ``doc://com.apple.foundation/documentation/Foundation/Stream/schedule(in:forMode:)`` and ``doc://com.apple.foundation/documentation/Foundation/Stream/remove(from:forMode:)``

Implement [`schedule(in:forMode:)`](/documentation/Foundation/Stream/schedule(in:forMode:)) to schedule the stream object on the specified run loop for the specified mode. Implement [`remove(from:forMode:)`](/documentation/Foundation/Stream/remove(from:forMode:)) to remove the object from the run loop. See the documentation of the [`RunLoop`](/documentation/Foundation/RunLoop) class for details. Once the stream object for an open stream is scheduled on a run loop, it is the responsibility of the subclass as it processes stream data to send [`stream(_:handle:)`](/documentation/Foundation/StreamDelegate/stream(_:handle:)) messages to its delegate.

- ``doc://com.apple.foundation/documentation/Foundation/Stream/property(forKey:)`` and ``doc://com.apple.foundation/documentation/Foundation/Stream/setProperty(_:forKey:)``

Implement these methods to return and set, respectively, the property value for the specified key. You may add custom properties, but be sure to handle all properties defined by [`Stream`](/documentation/Foundation/Stream) as well.

- ``doc://com.apple.foundation/documentation/Foundation/Stream/streamStatus`` and ``doc://com.apple.foundation/documentation/Foundation/Stream/streamError``

Implement [`streamStatus`](/documentation/Foundation/Stream/streamStatus) to return the current status of the stream as a [`Stream.Status`](/documentation/Foundation/Stream/Status) constant; you may define new [`Stream.Status`](/documentation/Foundation/Stream/Status) constants, but be sure to handle the system defined constants properly. Implement [`streamError`](/documentation/Foundation/Stream/streamError) to return an [`NSError`](/documentation/Foundation/NSError) object representing the current error. You might decide to return a custom [`NSError`](/documentation/Foundation/NSError) object that can provide complete and localized information about the error.

## Topics

### Creating Streams

[`getStreamsTo(_:port:inputStream:outputStream:)`](/documentation/Foundation/Stream/getStreamsTo(_:port:inputStream:outputStream:))

Creates and returns by reference an `NSInputStream` object and `NSOutputStream` object for a socket connection with a given host on a given port.

### Configuring Streams

[`property(forKey:)`](/documentation/Foundation/Stream/property(forKey:))

Returns the receiver’s property for a given key.

[`setProperty(_:forKey:)`](/documentation/Foundation/Stream/setProperty(_:forKey:))

Attempts to set the value of a given property of the receiver and returns a Boolean value that indicates whether the value is accepted by the receiver.

[`delegate`](/documentation/Foundation/Stream/delegate)

Sets the receiver’s delegate.

### Using Streams

[`open()`](/documentation/Foundation/Stream/open())

Opens the receiving stream.

[`close()`](/documentation/Foundation/Stream/close())

Closes the receiver.

### Managing Run Loops

[`schedule(in:forMode:)`](/documentation/Foundation/Stream/schedule(in:forMode:))

Schedules the receiver on a given run loop in a given mode.

[`remove(from:forMode:)`](/documentation/Foundation/Stream/remove(from:forMode:))

Removes the receiver from a given run loop running in a given mode.

### Getting Stream Information

[`streamStatus`](/documentation/Foundation/Stream/streamStatus)

Returns the receiver’s status.

[`streamError`](/documentation/Foundation/Stream/streamError)

Returns an `NSError` object representing the stream error.

### Constants

[`Stream.Status`](/documentation/Foundation/Stream/Status)

The type declared for the constants listed in <doc:stream/stream_status_constants>.

[Stream Status Constants](/documentation/Foundation/stream_status_constants)

These constants indicate the current status of a stream. They are returned by [`streamStatus`](/documentation/Foundation/Stream/streamStatus).

[`Stream.Event`](/documentation/Foundation/Stream/Event)

Describes the constants that may be sent to the delegate as a bit field in the second parameter of [`stream(_:handle:)`](/documentation/Foundation/StreamDelegate/stream(_:handle:)) to specify the kind of stream event.

[`StreamNetworkServiceTypeValue`](/documentation/Foundation/StreamNetworkServiceTypeValue)

`NSStream` defines these string constants for specifying the service type of a stream.

[`StreamSOCKSProxyConfiguration`](/documentation/Foundation/StreamSOCKSProxyConfiguration)

[`StreamSOCKSProxyVersion`](/documentation/Foundation/StreamSOCKSProxyVersion)

[`StreamSocketSecurityLevel`](/documentation/Foundation/StreamSocketSecurityLevel)

`NSStream` defines these string constants for specifying the secure-socket layer (SSL) security level.

[`Stream.PropertyKey`](/documentation/Foundation/Stream/PropertyKey)

`NSStream` defines these string constants as keys for accessing stream properties using [`property(forKey:)`](/documentation/Foundation/Stream/property(forKey:)) and setting properties with [`setProperty(_:forKey:)`](/documentation/Foundation/Stream/setProperty(_:forKey:)):

[`NSStreamSocketSSLErrorDomain`](/documentation/Foundation/NSStreamSocketSSLErrorDomain)

The error domain used by `NSError` when reporting SSL errors.

[`NSStreamSOCKSErrorDomain`](/documentation/Foundation/NSStreamSOCKSErrorDomain)

The error domain used by `NSError` when reporting SOCKS errors.



---

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)
