Specific introduction of Ios:nsbundle

Source: Internet
Author: User

NSBundle Introduction: It is a singleton class that is used to load resources

A bundle is a directory that contains the resources that the program will use. These resources include like, sound, compiled code, NIB files (the user will also refer to bundles as plug-in). The corresponding BUNDLE,COCOA provides class NSBundle.

Our program is a bundle. In the Finder, an application looks no different from other files. But it's actually a directory that contains nib files, compiled code, and other resources. We call this directory the program's main bundle.

Some of the resources in the bundle can be localized. For example, for foo.nib, we can have two versions: one for English-speaking users and one for French-speaking users. There will be two subdirectories in the bundle: English.lproj and French.lproj, where we put the respective versions of the foo.nib file. When the program needs to load the foo.nib file, the bundle is automatically loaded according to the language set. We will discuss the localization in detail in Chapter 16

Get the program's main bundle by using the following method
NSBundle *mybundle = [NSBundle mainbundle];

In general, we use this method to get bundles. If you need resources for another directory, you can specify a path to get the bundle
NSBundle *goodbundle;
Goodbundle = [NSBundle bundlewithpath:@ "~/.myapp/good.bundle"];

Once we have the NSBundle object, we have access to the resources in it.
Extension is optional
NSString *path = [Goodbundle pathforimageresource:@ "Mom"];
Nsimage *momphoto = [[Nsimage alloc] initwithcontentsoffile:path];

The bundle can contain a library. If we get a class from the library, the bundle connects to the library and looks for that class:
Class Newclass = [Goodbundle classnamed:@ "Rover"];
ID newinstance = [[Newclass alloc] init];

If you do not know the class name, you can also find the main class to get
Class AClass = [Goodbundle principalclass];
ID aninstance = [[AClass alloc] init];

As you can see, NSBundle has a lot of uses. In this, NSBundle is responsible for loading the nib file (in the background). We can also load nib files without Nswindowcontroller, using NSBundle directly:
BOOL successful = [NSBundle loadnibnamed:@ "about" owner:someobject];
Note Oh, we've specified an object someobject as the nib file's Owner

When using Initwithcontentsoffile, the file path is written using the Initwithcontentsoffile method to initialize the object by reading the contents of a file. But how should the path of the file be determined? You can use the NSBundle object to get it. For example, the current program in the directory has a file re.xml, we want to make the contents of the file as a NSData data source to initialize a NSData object, can be implemented in the following way: NSString *filepath = [[NSBundle Mainbundle] pathforresouse:@ "Re" oftype:@ "xml"; NSData *data = [[NSData alloc] initwithcontentsoffile:filepath]; Read the contents of plist: NSString *datapath = [[NSBundle Mainbundle] pathforresource:@ "Data" oftype:@ "plist"]; Self.data = [Nsarray arraywithcontentsoffile:datapath]; Delete local file NSString * thepath=[self Getuserdocumentdirectorypath]; Nsmutablestring * fullpath=[[[nsmutablestring Alloc]init]autorelease]; [FullPath Appendstring:thepath]; NSString * Idstring=[idarray ObjectAtIndex:indexPath.row]; NSString * covername=[nsstring stringwithformat:@ "/%@.jpg", idstring]; [FullPath Appendstring:covername]; Nsfilemanager *defaultmanager;defaultmanager = [Nsfilemanager defaultmanager];-(BOOL) removeItemAtPath: (NSString *) Path Error: (Nserror *) error BOOL Boolvalue=[defaultmanager removeItemAtPath: FullPath error:nil];if (boolvalue) {NSLog (@ "Remove cover image OK"); -(nsstring*) Getuserdocumentdirectorypath {nsarray* array = nssearchpathfordirectoriesindomains (NSDocumentDirectory , Nsuserdomainmask,yes), if ([array count]) return [array objectatindex:0];else return @ "";}
Summary:Here's a note: instead of using any relative path directly in future development, use the absolute path after the calculation.

I. Getting pictures

1. NSString *path = [[Nsbuddle mainbuddle] pathforresource:@ "ResourceName"

[Email protected] "resourcetype"];

UIImage *image = [[UIImage Imagewithcontentsoffile:path];

2. UIImage *image = [UIImage imagenamed:@ "ImageName"];

Load Picture:

    1. Method 1
    2. UIImage *IMAG1 = [UIImage imagenamed:@ "Image.png"]; Loading the picture in this way is cached and does not need to be loaded the next time it is used, but the disadvantage is that the application consumes memory.
    3. Method 2
    4. A good solution to the memory cache problem
    5. UIImage *image2 = [UIImage imagewithcontentsoffile:[[nsbundle mainbundle] pathforresource:@ "Image.png" OfType:nil]];
    6. Method 3
    7. A good solution to the memory cache problem
    8. NSData *imagedata = [NSData datawithcontentsoffile:[[nsbundle mainbundle] pathforresource:@ "Image.png" OfType:nil]];
    9. UIImage *image3 = [UIImage imagewithdata:imagedata];

Two. Get the plist file

Nsarray *array =[[nsarray alloc]initwithcontentsoffile:[[nsbundlemainbundle]pathforresource:@ "name" ofType:@ "plist "]];

Nsdictionary *dict=[arrayobjectatindex:index];//Convert the contents of the plist file into a dictionary

Specific introduction of Ios:nsbundle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.