2014.12.6 nsbundle Details of Use

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 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 nsbundle *mybundle = [NSBundle Mainbundle] by using the following method:

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

Once we have the NSBundle object, we can access 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, directly using nsbundle:bool successful = [NSBundle loadnibnamed:@ "about" Owner:someobject] ; Note Oh, we've specified an object someobject as the nib file's Owner

使用initWithContentsOfFile时,文件路径的写法 使用initWithContentsOfFile方法可以通过读取一个文件的内容来初始化对象。 但文件的路径应该怎么确定呢? 可以使用NSBundle的对象来获取。 例如当前程序所在目录下有个文件re.xml,我们要将该文件的内容做为NSData的数据源来初始化一个NSData对象,可以用下面的方法来实现:
NSString*filePath = [[NSBundle mainBundle] pathForResouse:@"re" ofType:@"xml"]; NSData *data = [[NSDataalloc] initWithContentsOfFile:filePath];
读取plist中的内容:
NSString*dataPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]; self.data = [NSArrayarrayWithContentsOfFile:dataPath];
删除本地文件
NSString* thePath=[selfgetUserDocumentDirectoryPath];
NSMutableString* fullPath=[[[NSMutableStringalloc]init]autorelease];
[fullPath appendString:thePath];
NSString* idString=[idArray objectAtIndex:indexPath.row];
NSString* coverName=[NSStringstringWithFormat:@"/%@.jpg",idString];
[fullPath appendString:coverName];
NSFileManager*defaultManager;
defaultManager = [NSFileManagerdefaultManager];
- (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];
elsereturn@"";
}

2014.12.6 nsbundle Details of Use

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.