The bundle’s principal class.
- iOS 2.0+
- macOS 10.0+
- UIKit for Mac 13.0+Beta
- tvOS 9.0+
- watchOS 2.0+
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
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
info, 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). The bundle obtains the principal class from the dictionary using the key
NSPrincipal. For non-loadable bundles (applications and frameworks), if the principal class is not specified in the property list, this property is
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:
The 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
did after all classes and categories have been loaded; see Notifications for details.
The following method obtains a bundle by specifying its path (
bundle), then loads the bundle with
principal and uses the principal class object to allocate and initialize an instance of that class: