principalClass
The bundle’s principal class.
Declaration
var principalClass: AnyClass? { get }Discussion
This property is set after ensuring that the code containing the definition of the class is dynamically loaded. If the bundle encounters errors in loading or if it can’t find the executable code file in the bundle directory, this property is nil.
The principal class typically controls all the other classes in the bundle; it should mediate between those classes and classes external to the bundle. Classes (and categories) are loaded from just one file within the bundle directory. The bundle obtains the name of the code file to load from the dictionary returned from infoDictionary, using “NSExecutable” as the key. The bundle determines its principal class in one of two ways:
It first looks in its own information dictionary, which extracts the information encoded in the bundle’s property list (
Info.plist). The bundle obtains the principal class from the dictionary using the keyNSPrincipalClass. For non-loadable bundles (applications and frameworks), if the principal class is not specified in the property list, this property isnil.If the principal class is not specified in the information dictionary, the bundle identifies the first class loaded as the principal class. When several classes are linked into a dynamically loadable file, the default principal class is the first one listed on the
ldcommand line. In the following example, Reporter would be the principal class:
ld -o myBundle -r Reporter.o NotePad.o QueryList.oThe order of classes in Xcode’s project browser is the order in which they will be linked. To designate the principal class, control-drag the file containing its implementation to the top of the list.
As a side effect of code loading, the receiver posts didLoadNotification after all classes and categories have been loaded; see Notifications for details.
The following method obtains a bundle by specifying its path (bundleWithPath:), then loads the bundle with principalClass and uses the principal class object to allocate and initialize an instance of that class:
- (void)loadBundle:(id)sender
{
Class exampleClass;
id newInstance;
NSString *path = @"/tmp/Projects/BundleExample/BundleExample.bundle";
NSBundle *bundleToLoad = [NSBundle bundleWithPath:path];
if (exampleClass = bundleToLoad.principalClass) {
newInstance = [[exampleClass alloc] init];
[newInstance doSomething];
}
}