---
title: "setResourceValue(_:forKey:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nsurl/setresourcevalue(_:forkey:)"
---

# setResourceValue(_:forKey:)

Sets the URL’s resource property for a given key to a given value.

## Declaration

```swift
func setResourceValue(_ value: Any?, forKey key: URLResourceKey) throws
```

## Parameters

- `value`: The value for the resource property defined by key.
- `key`: The name of one of the URL’s resource properties.

## Mentioned in

Using the file system effectively

## Discussion

Discussion This method synchronously writes the new resource value out to disk. Attempts to set a read-only resource property or to set a resource property that is not supported by the resource are ignored and are not considered errors. If an error occurs, this method returns false and populates the object pointer referenced by error with additional information. note: This method applies only to URLs for file system resources. note: In Swift, this method returns Void 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 in The Swift Programming Language and About Imported Cocoa Error Parameters.

## See Also

### Accessing Resource Values

- [resourceValues(forKeys:)](foundation/nsurl/resourcevalues(forkeys:).md)
- [getResourceValue(_:forKey:)](foundation/nsurl/getresourcevalue(_:forkey:).md)
- [setResourceValues(_:)](foundation/nsurl/setresourcevalues(_:).md)
- [removeAllCachedResourceValues()](foundation/nsurl/removeallcachedresourcevalues().md)
- [removeCachedResourceValue(forKey:)](foundation/nsurl/removecachedresourcevalue(forkey:).md)
- [setTemporaryResourceValue(_:forKey:)](foundation/nsurl/settemporaryresourcevalue(_:forkey:).md)
- [URLResourceKey](foundation/urlresourcekey.md)
