Monday, April 29, 2013

NSBundle pathForResource example ios


pathForResource :ofType:

Returns the full pathname for the resource identified by the specified name and file extension.
- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)extension
Parameters
name
The name of the resource file. If name is an empty string or nil, returns the first file encountered of the supplied type.
extension
If extension is an empty string or nil, the extension is assumed not to exist and the file is the first file encountered that exactly matches name.
Return Value of [NSBundle pathForResource]
The full pathname for the resource file or nil if the file could not be located.
Discussion of [NSBundle pathForResource]
The method first looks for a matching resource file in the non-localized resource directory of the specified bundle. (In OS X, this directory is typically called Resources but in iOS, it is the main bundle directory.) If a matching resource file is not found, it then looks in the top level of any available language-specific “.lproj” directories. (The search order for the language-specific directories corresponds to the user’s preferences.) It does not recurse through other subdirectories at any of these locations. For more details see Internationalization Programming Topics.
Example of [NSBundle pathForResource]
The following code fragment gets the path to a plist within the bundle, and loads it into anNSDictionary.
 
NSBundle *thisBundle = [NSBundle bundleForClass:[self class]];
if (commonDictionaryPath = [thisBundle pathForResource :@"CommonDictionary" ofType:@"plist"])  {
    theDictionary = [[NSDictionary alloc] initWithContentsOfFile:commonDictionaryPath];
}