<!--
{
  "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/NSString/init(bytesNoCopy:length:encoding:freeWhenDone:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSString(im)initWithBytesNoCopy:length:encoding:freeWhenDone:"
  },
  "title" : "init(bytesNoCopy:length:encoding:freeWhenDone:)"
}
-->

# init(bytesNoCopy:length:encoding:freeWhenDone:)

Returns an initialized `NSString` object that contains a given number of bytes from a given buffer of bytes interpreted in a given encoding, and optionally frees the buffer.

```
convenience init?(bytesNoCopy bytes: UnsafeMutableRawPointer, length len: Int, encoding: UInt, freeWhenDone freeBuffer: Bool)
```

## Parameters

`bytes`

A buffer of bytes interpreted in the encoding specified by `encoding`.

`len`

The number of bytes to use from `bytes`.

`encoding`

The character encoding of `bytes`. For possible values, see [`NSStringEncoding`](/documentation/Foundation/NSStringEncoding).

`freeBuffer`

If <doc://com.apple.documentation/documentation/Swift/true>, the receiver releases the memory with `free()` when it no longer needs the data; if <doc://com.apple.documentation/documentation/Swift/false> it won’t.

## Return Value

An initialized `NSString` object containing `length` bytes from `bytes` interpreted using the encoding `encoding`. The returned object may be different from the original receiver.

## Discussion

If an error occurs during the creation of the string, then `bytes` isn’t freed even if `flag` is <doc://com.apple.documentation/documentation/Swift/true>. In this case, the caller is responsible for freeing the buffer. This allows the caller to continue trying to create a string with the buffer, without having the buffer deallocated.

---

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)