<!--
{
  "availability" : [
    "macOS: 10.7.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSFileVersion/addOfItem(at:withContentsOf:options:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSFileVersion(cm)addVersionOfItemAtURL:withContentsOfURL:options:error:"
  },
  "title" : "addOfItem(at:withContentsOf:options:)"
}
-->

# addOfItem(at:withContentsOf:options:)

Creates a version of the file at the specified location.

```
class func addOfItem(at url: URL, withContentsOf contentsURL: URL, options: NSFileVersion.AddingOptions = []) throws -> NSFileVersion
```

## Parameters

`url`

The location at which to store the new file version.

`contentsURL`

The URL that specifies the file to use for the version contents.

`options`

Specify 0 for this parameter if you want to create a copy of the file at the location specified by the `url` parameter. Alternatively, specify one of the constants described in [`NSFileVersion.AddingOptions`](/documentation/Foundation/NSFileVersion/AddingOptions).

## Return Value

The file version object representing the new version or `nil` if an error occurred.

## Discussion

You can use this method to save a version of your file to the location specified by the `url` parameter. The contents of the file are taken from the `contentsURL` parameter, whose value may be the same as the `url` parameter.

You should always add file versions as part of a coordinated write operation to a file. In other words, always call this method from a block passed to a file coordinator object to initiate a write operation. Doing so ensures that no other processes are operating on the file while you save the version to its new location.

> 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)