<!--
{
  "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/UserDefaults/string(forKey:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSUserDefaults(im)stringForKey:"
  },
  "title" : "string(forKey:)"
}
-->

# string(forKey:)

Returns the string associated with the specified key.

```
func string(forKey defaultName: String) -> String?
```

## Parameters

`defaultName`

The key to retrieve from the defaults database.

## Return Value

The string associated with `defaultName`, or `nil` if the key isn’t present
in the defaults database.

## Discussion

This method automatically coerces certain types to equivalent strings. For example,
it converts the integer `123` to the string “123”. If the key is present, but the method
can’t use it to create an appropriate string, this method returns `nil`.

This returned string is immutable, even if you originally set the key to a mutable string.

---

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)