---
title: "addRegularFile(withContents:preferredFilename:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/filewrapper/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.

## Declaration

```swift
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

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 in File System Programming Guide for more information about the file-wrapper list structure.

## Discussion

Discussion This is a convenience method. The default implementation allocates a new file wrapper, initializes it with init(regularFileWithContents:), set its preferredFilename property, adds it to the directory with addFileWrapper(_:), and returns what 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.

## See Also

### Accessing File-Wrapper Information

- [fileWrappers](foundation/filewrapper/filewrappers.md)
- [addFileWrapper(_:)](foundation/filewrapper/addfilewrapper(_:).md)
- [removeFileWrapper(_:)](foundation/filewrapper/removefilewrapper(_:).md)
- [addFile(withPath:)](foundation/filewrapper/addfile(withpath:).md)
- [addSymbolicLink(withDestination:preferredFilename:)](foundation/filewrapper/addsymboliclink(withdestination:preferredfilename:).md)
- [keyForChildFileWrapper(_:)](foundation/filewrapper/keyforchildfilewrapper(_:).md)
- [symbolicLinkDestination()](foundation/filewrapper/symboliclinkdestination().md)
- [symbolicLinkDestinationURL](foundation/filewrapper/symboliclinkdestinationurl.md)
