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

# init(data:encoding:)

Returns an `NSString` object initialized by converting given data into UTF-16 code units using a given encoding.

```
convenience init?(data: Data, encoding: UInt)
```

## Parameters

`data`

An `NSData` object containing bytes in `encoding` and the default plain text format (that is, pure content with no attributes or other markups) for that encoding.

`encoding`

The encoding used by `data`. For possible values, see [`NSStringEncoding`](/documentation/Foundation/NSStringEncoding).

## Return Value

An `NSString` object initialized by converting the bytes in `data` into UTF-16 code units using `encoding`. The returned object may be different from the original receiver. Returns `nil` if the initialization fails for some reason (for example if `data` does not represent valid data for `encoding`).

## Discussion

---

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)