path(forResource:ofType:inDirectory:)
Returns the full pathname for the resource identified by the specified name and file extension and located in the specified bundle subdirectory.
Declaration
func path(forResource name: String?, ofType ext: String?, inDirectory subpath: String?) -> String?Parameters
- name:
The name of the resource file.
If you specify
nil, the method returns the first resource file it finds that matches the remaining criteria. - ext:
The filename extension of the files to locate.
If you specify an empty string or
nil, all the files insubpathand its subdirectories are returned. If an extension is provided the subdirectories are not searched. - subpath:
The name of the bundle subdirectory.
Return Value
The full pathname for the resource file, or nil if the file could not be located.
Discussion
If subpath is nil, this method searches the top-level nonlocalized resource directory and the top-level of any language-specific directories. (In macOS, the top-level nonlocalized resource directory is typically called Resources but in iOS, it is the main bundle directory.) For example, suppose you have a Mac app with a modern bundle and you specify @"Documentation" for the subpath parameter. This method would first look in the Contents/Resources/Documentation directory of the bundle, followed by the Documentation subdirectories of each language-specific .lproj directory.
Whether this method recurses through subdirectories is dependent on the extension parameter. If nil or an empty string it will recurse, otherwise, it does not. (The search order for the language-specific directories corresponds to the user’s preferences.) For details on how localized resources are found, read The Bundle Search Pattern in Bundle Programming Guide.
See Also
Related Documentation
Finding Resource Files
url(forResource:withExtension:subdirectory:)url(forResource:withExtension:)urls(forResourcesWithExtension:subdirectory:)url(forResource:withExtension:subdirectory:localization:)urls(forResourcesWithExtension:subdirectory:localization:)url(forResource:withExtension:subdirectory:in:)urls(forResourcesWithExtension:subdirectory:in:)path(forResource:ofType:)path(forResource:ofType:inDirectory:forLocalization:)paths(forResourcesOfType:inDirectory:)paths(forResourcesOfType:inDirectory:forLocalization:)path(forResource:ofType:inDirectory:)paths(forResourcesOfType:inDirectory:)