What is NSBundle and mainBundle in objective C?

孤街醉人 提交于 2019-12-02 18:57:46
JeremyP

A bundle is a structure used for packaging software on Mac OS X. Applications, frameworks and plug-ins are all different kinds of bundles. Bundles may contain executable code, resources, header files and other stuff (including other bundles) if you so wish.

Bundles are implemented as directory trees with a defined structure. Applications, frameworks and plug-ins each have their own variation on the structure of the tree. However, to the Finder, bundles look like single files.

The main bundle is simply the bundle of the application that is running. So, for instance, the main bundle of the Apple mail program is /Applications/Mail.app.

Shan

Suppose our code is:

NSString *myFile=[[NSBundle mainBundle]pathForResource:@"subjects" ofType:@"plist"];

We create subject.plist in Xcode (File> new> new file> iOS> resource> property list) and copy it into our supporting folder.

This is really a way getting to the installed file system on the iOS device when we do not know what it is. It's a way to obtain our subject.plist file which is a part of our internal Bundle or internal package to our application. We don't have direct access to a file system on an iOS device so this is the way we get hold of our own assets.

There is a good apple documentation about bundles. Bundle Programming Guide

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!