<!--
{
  "availability" : [
    "iOS: 2.0.0 - 2.0.0",
    "iPadOS: 2.0.0 - 2.0.0",
    "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/string(withContentsOfFile:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSString(cm)stringWithContentsOfFile:"
  },
  "title" : "string(withContentsOfFile:)"
}
-->

# string(withContentsOfFile:)

Returns a string created by reading data from the file named by a given path.

```
class func string(withContentsOfFile path: String) -> Any?
```

## Discussion

If the contents begin with a Unicode byte-order mark (`U+FEFF` or `U+FFFE`), interprets the contents as UTF-16 code units. If the contents begin with a UTF-8 byte-order mark (`EFBBBF`), interprets the contents as UTF-8. Otherwise, interprets the contents as data in the default C string encoding. Since the default C string encoding will vary with the user’s configuration, do not depend on this method unless you are using Unicode or UTF-8 or you can verify the default C string encoding. Returns `nil` if the file can’t be opened.

## See Also

[`+  stringWithContentsOfFile:encoding:error:`](/documentation/Foundation/NSString/stringWithContentsOfFile:encoding:error:)

Returns a string created by reading data from the file at a given path interpreted using a given encoding.

[`+  stringWithContentsOfFile:usedEncoding:error:`](/documentation/Foundation/NSString/stringWithContentsOfFile:usedEncoding:error:)

Returns a string created by reading data from the file at a given path and returns by reference the encoding used to interpret the 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)