<!--
{
  "availability" : [
    "iOS: 5.0.0 -",
    "iPadOS: 5.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.7.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSFileVersion/replaceItem(at:options:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSFileVersion(im)replaceItemAtURL:options:error:"
  },
  "title" : "replaceItem(at:options:)"
}
-->

# replaceItem(at:options:)

Replace the contents of the specified file with the contents of the current version’s file.

```
func replaceItem(at url: URL, options: NSFileVersion.ReplacingOptions = []) throws -> URL
```

## Parameters

`url`

The file whose contents you want to replace. If the file at this URL does not exist, a new file is created at the location.

`options`

Specify `0` to overwrite the file in place; otherwise, specify one of the constants described in [`NSFileVersion.ReplacingOptions`](/documentation/Foundation/NSFileVersion/ReplacingOptions).

## Return Value

The URL of the file that was written, which may be different than the one specified in the `url` parameter.

## Discussion

When replacing the contents of the file, this method does not normally replace the display name associated with the file. The only exception is when the file at `url` is of a different type than the file associated with this version object. In such a case, the file name remains the same but its filename extension changes to match the type of the new contents. (Of course, if filename extension hiding is enabled, this change is not noticeable to users.)

> Handling Errors in Swift:
> In Swift, this method returns a nonoptional result and is marked with the `throws` keyword to indicate that it throws an error in cases of failure.
> 
> You call this method in a `try` expression and handle any errors in the `catch` clauses of a `do` statement, as described in [Error Handling](https://docs.swift.org/swift-book/LanguageGuide/ErrorHandling.html) in [The Swift Programming Language](https://docs.swift.org/swift-book/) and `About Imported Cocoa Error Parameters`.

---

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)