Bundle paths used in iOS development

Source: Internet
Author: User

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 the 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.

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.

A nsbundle bundle, which is a specific type of file in which the content follows a specific structure.

One of the main functions of nsbundle is to obtain resources in the Resource folder .

When using [NSData datawithcontentoffile:@ "foo"] in programming, it is always impossible to read the correct file contents. It is possible to use [NSData datawithcontentoffile:[[nsbundle mainbundle] pathforresource:@ "foo" oftype:@ "".

Because when using relative path, in fact his relative current directory is not the directory that the program runs, but "/". Only the path generated using [NSBundle Mainbundle] is the true path to the file. 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"];

Two. Get the plist file

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

Nsdictionary *dict=[arrayobjectatindex:index];//Convert the contents of the plist file into a dictionary-------turn (http://blog.csdn.net/bihailantian1988 /article/details/7703358)

Finally remember: the path used by iOS development is the bundle path

Bundle paths used in iOS development

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.