One of the main reasons to get a bundle object is so that you can load resources from your bundle. Both CFBundle
and NSBundle
provide interfaces for finding specific resource types in your bundle. These interfaces return a path to the resource that you can then use to load it.
Resource types are typically specified using filename extensions, not the file type information used in previous versions of Mac OS; therefore, you must make sure your files are named properly. For Core Foundation programs, the most commonly used functions for finding resources are CFBundleCopyResourceURL
and CFBundleCopyResourceURLsOfType
. These functions let you retrieve resources using name, type, and directory information. Similarly, Cocoa applications typically use the pathForResource:ofType:
method and pathForResourcesOfType:InDirectory:
methods to retrieve resources.
Important: CFBundle
and NSBundle
consider case when searching for resource files in the bundle directory. This case-sensitive search occurs even on file systems (such as HFS+) that are not case sensitive when it comes to file names.
Even if you do not have a bundle object, you can still load resources from bundles whose paths you know. Both Core Foundation and Cocoa provide API for searching bundles with only a path to the bundle. However, it is important to remember that searching a bundle for multiple resources is always faster using a bundle object. The bundle objects cache search information as they go, so subsequent searches are usually faster.
The Core Foundation and Cocoa API take into account localized versions of resources when determining which paths to return. For information on how these interfaces determine which files to return, see “Searching for Bundle Resources.”
Suppose you have placed an image called Seagull.jpg
in your application’s main bundle. Listing 1 shows you how to search for this image by name and type using the Core Foundation function CFBundleCopyResourceURL
. In this case, the code searches for the file named “Seagull” with the file type (filename extension) of “jpg”. This example searches for the resource starting at the top level of the bundle’s Resources
directory.
Listing 1 Locating resources inside a bundle by name and type
CFURLRef seagullURL; |
// Look for a resource in the main bundle by name and type. |
seagullURL = CFBundleCopyResourceURL( mainBundle, |
CFSTR("Seagull"), |
CFSTR("jpg"), |
NULL ); |
Suppose that instead of searching for one image file, you wanted to get the names of all image files in a directory called BirdImages
. You could load all of the JPEGs in the directory using the function CFBundleCopyResourceURLsOfType
, as shown in Listing 2.
Listing 2 Locating multiple resources by type
CFArrayRef birdURLs; |
// Find all of the JPEG images in a given directory. |
birdURLs = CFBundleCopyResourceURLsOfType( mainBundle, |
CFSTR("jpg"), |
CFSTR("BirdImages") ); |
Note: You can search for resources that do not have a filename extension. To get the path to such a resource, specify the complete name of the resource and specify NULL
for the resource type.
© 2003, 2005 Apple Computer, Inc. All Rights Reserved. (Last updated: 2005-11-09)