<!--
{
  "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/NSError/init(domain:code:userInfo:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSError(im)initWithDomain:code:userInfo:"
  },
  "title" : "init(domain:code:userInfo:)"
}
-->

# init(domain:code:userInfo:)

Returns an `NSError` object initialized for a given domain and code with a given `userInfo` dictionary.

```
init(domain: String, code: Int, userInfo dict: [String : Any]? = nil)
```

## Parameters

`domain`

The error domain—this can be one of the predefined `NSError` domains, or an arbitrary string describing a custom domain. `domain` must not be `nil`. See `Error Domains` for a list of predefined domains.

`code`

The error code for the error.

`dict`

The `userInfo` dictionary for the error. `userInfo` may be `nil`.

## Return Value

An `NSError` object initialized for `domain` with the specified error `code` and the dictionary of arbitrary data `userInfo`.

## Discussion

This is the designated initializer for `NSError`.

## See Also

[`errorWithDomain:code:userInfo:`](/documentation/Foundation/NSError/errorWithDomain:code:userInfo:)

Creates and initializes an `NSError` object for a given domain and code with a given `userInfo` dictionary.



---

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)