Monday, April 29, 2013

NSBundle mainBundle example ios


mainBundle

Returns the NSBundle object that corresponds to the directory where the current application executable is located.
+ (NSBundle *)mainBundle
Return Value of [NSBundle mainBundle]
The NSBundle object that corresponds to the directory where the application executable is located, or nil if a bundle object could not be created.
Discussion of [NSBundle mainBundle]
This method allocates and initializes a bundle object if one doesn’t already exist. The new object corresponds to the directory where the application executable is located. Be sure to check the return value to make sure you have a valid bundle. This method may return a valid bundle object even for unbundled applications.[NSBundle mainBundle]
In general, the main bundle corresponds to an application file package or application wrapper: a directory that bears the name of the application and is marked by a “.app” extension.
Example of [NSBundle mainBundle] - 1
    totalCount = 0;
    NSArray *d = [[NSBundle mainBundle] pathsForResourcesOfType:@"jpg" inDirectory:nil];
    for(NSString *s in d)
    {
       if([[s lastPathComponent] hasPrefix:@"image_"]){
       totalCount++;
       }
    }
Example of [NSBundle mainBundle] - 2
NSString *bundlePath = [[NSBundle mainBundle] resourcePath];
NSString *secondParentPath = [[bundlePath stringByDeletingLastPathComponent] stringByDeletingLastPathComponent];