set(_:forKey:)
Sets the value of the specified key to a URL.
Declaration
func set(_ url: URL?, forKey defaultName: String)Parameters
- url:
The value to store in the defaults database.
- defaultName:
The key that contains the setting’s name.
Discussion
This method handles file URLs differently than other types of URLs. For most URLs, the method stores the URL object as the root object of a Data archive. If url contains a path to a file in the home directory, this method replaces the home directory portion of the path with a tilde (~) character before generating the data object. When you read the value back, the system expands the tilde character to the current home directory path.
If the location of a file might change, don’t use a file URL to specify its location. Instead, create a bookmark URL using the bookmarkData(withContentsOf:) method and save that URL instead. Bookmark URLs store additional information about the file so the system can locate the file later, even if the path to that file changes.
After you call this method, the system generates a didChangeNotification for registered observers.