<!--
{
  "availability" : [
    "iOS: 2.0.0 - 2.0.0",
    "iPadOS: 2.0.0 - 2.0.0",
    "swift: -",
    "tvOS: 9.0.0 - 9.0.0",
    "visionOS: 1.0.0 - 1.0.0",
    "watchOS: 2.0.0 - 2.0.0"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSString/init(CStringNoCopy:length:freeWhenDone:)-86dm2",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSString(im)initWithCStringNoCopy:length:freeWhenDone:::SYNTHESIZED::c:objc(cs)NSString"
  },
  "title" : "init(CStringNoCopy:length:freeWhenDone:)"
}
-->

# init(CStringNoCopy:length:freeWhenDone:)

Initializes the receiver, a newly allocated `NSString` object, by converting the data in a given C-string from the default C-string encoding into the Unicode character encoding.

```
convenience init?(CStringNoCopy bytes: UnsafeMutablePointer<CChar>, length: Int, freeWhenDone freeBuffer: Bool)
```

## Discussion

This method converts `length` * `sizeof(char)` bytes from `cString` and doesn’t stop short at a zero character. `cString` must contain data in the default C-string encoding and may not be `NULL`. The receiver becomes the owner of `cString`; if `flag` is <doc://com.apple.documentation/documentation/Swift/true> it will free the memory when it no longer needs it, but if `flag` is <doc://com.apple.documentation/documentation/Swift/false> it won’t. Returns an initialized object, which might be different from the original receiver.

You can use this method to create an immutable string from an immutable (`const char *`) C-string buffer. If you receive a warning message, you can disregard it; its purpose is simply to warn you that the C string passed as the method’s first argument may be modified. If you make certain the `freeWhenDone` argument to `initWithStringNoCopy` is <doc://com.apple.documentation/documentation/Swift/false>, the C string passed as the method’s first argument cannot be modified, so you can safely use `initWithStringNoCopy` to create an immutable string from an immutable (`const char *`) C-string buffer.

## See Also



---

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)