<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/Bundle/principalClass",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Property",
  "symbol" : {
    "kind" : "Instance Property",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSBundle(py)principalClass"
  },
  "title" : "principalClass"
}
-->

# principalClass

The bundle’s principal class.

```
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`](/documentation/Foundation/Bundle/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 key `NSPrincipalClass`. For non-loadable bundles (applications and frameworks), if the principal class is not specified in the property list, this property is `nil`.
- 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 `ld` command line. In the following example, Reporter would be the principal class:

```objc
ld -o myBundle -r Reporter.o NotePad.o QueryList.o
```

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 [`didLoadNotification`](/documentation/Foundation/Bundle/didLoadNotification) after all classes and categories have been loaded; see `Notifications` for details.

The following method obtains a bundle by specifying its path ([`bundleWithPath:`](/documentation/Foundation/NSBundle/bundleWithPath:)), then loads the bundle with [`principalClass`](/documentation/Foundation/Bundle/principalClass) and uses the principal class object to allocate and initialize an instance of that class:

```objc
- (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];
    }
}
```

## See Also

[`-  load`](/documentation/Foundation/Bundle/load())

Dynamically loads the bundle’s executable code into a running program, if the code has not already been loaded.

[`infoDictionary`](/documentation/Foundation/Bundle/infoDictionary)

A dictionary, constructed from the bundle’s `Info.plist` file, that contains information about the receiver.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)