<!--
{
  "availability" : [
    "iOS: 4.0.0 -",
    "iPadOS: 4.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/FileWrapper/addRegularFile(withContents:preferredFilename:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSFileWrapper(im)addRegularFileWithContents:preferredFilename:"
  },
  "title" : "addRegularFile(withContents:preferredFilename:)"
}
-->

# addRegularFile(withContents:preferredFilename:)

Creates a regular-file file wrapper with the given contents and adds it to the receiver, which must be a directory file wrapper.

```
func addRegularFile(withContents data: Data, preferredFilename fileName: String) -> String
```

## Parameters

`data`

Contents for the new regular-file file wrapper.

`fileName`

Preferred filename for the new regular-file file wrapper.

## Return Value

Dictionary key used to store the new file wrapper in the directory’s list of file wrappers. The dictionary key is a unique filename, which is the same as the passed-in file wrapper’s preferred filename unless that name is already in use as a key in the directory’s dictionary of children. See [Accessing File Wrapper Identities](https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileWrappers/FileWrappers.html#//apple_ref/doc/uid/TP40010672-CH13-SW1) in [File System Programming Guide](https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40010672) for more information about the file-wrapper list structure.

## Discussion

This is a convenience method. The default implementation allocates a new file wrapper, initializes it with [`init(regularFileWithContents:)`](/documentation/Foundation/FileWrapper/init(regularFileWithContents:)), set its [`preferredFilename`](/documentation/Foundation/FileWrapper/preferredFilename) property, adds it to the directory with [`addFileWrapper(_:)`](/documentation/Foundation/FileWrapper/addFileWrapper(_:)), and returns what [`addFileWrapper(_:)`](/documentation/Foundation/FileWrapper/addFileWrapper(_:)) returned.

### Special Considerations

This method raises `NSInternalInconsistencyException` if the receiver is not a directory file wrapper.

This method raises `NSInvalidArgumentException` if you pass `nil` or an empty value for `filename`.

---

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)