Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

principal class of NSBundle

Plz help me to understand what is principalClass used for?what is the syntax of it. i understand tat it is in NSBundle Class,but can we create it for any bundles of is it specific only for laodable bundles? plz help me to know the concept of principalClass.

Thanking You.

like image 377
suse Avatar asked Dec 28 '09 03:12

suse


2 Answers

The "principal class" of a bundle is merely the Objective-C class that is marked as the primary class of the bundle and, thusly, will be returned by the -principalClass method of the bundle instance.

Nothing more, nothing less and there is no magic.

It only exists for loadable bundles because only loadable bundles define new Objective-C classes.

like image 124
bbum Avatar answered Sep 30 '22 13:09

bbum


I will give you an example of how you can create and load a bundle as plugin. Hope that this will help you a lot. I must say that I agree with the other 2 (so far) answers. So...

Create an Xcode project as "Bundle" (in Xcode 3.2.6 is in New Project->Framework & Library-> select "Bundle"). Create the following files...

PClass.h

#import <Foundation/Foundation.h>
@interface PClass : NSObject {

}

- (NSString*) stringMessage;

@end

PClass.m

- (NSString*) stringMessage {
    return @"Hallo from plugin";
}

in the projects .plist file add the following two entries:

"Bundle display name" "MyPlugin"

"Principal class" "PClass"

Then compile the project and move the binary (.../build/Debug/yourPlugin.bundle) to a folder you like to keep your plugins of a project of yours (may be copied into aProject.app/Plugins/ with a little extra care).

Then to an already Xcode project add the following:

- (void) loadPlugin {

    id bundle = [NSBundle bundleWithPath:@"the path you/placed/yourPlugin.bundle"];

    NSLog(@"%@", [[bundle infoDictionary] valueForKey:@"CFBundleDisplayName"]);
    // Here you can preview your plugins names without loading them if you don't need to or just to
    // display it to GUI, etc

    NSError *err;
    if(![bundle loadAndReturnError:&err]) {
        // err 
    } else {
        // bundle loaded
        Class PluginClass = [bundle principalClass]; // here is where you need your principal class!
        // OR...
        //Class someClass = [bundle classNamed:@"KillerAppController"];

        id instance = [[PluginClass alloc] init];

        NSLog(@"%@", [instance stringMessage]);

        [instance release];  // If required
    [bundle unload]; // If required
}

}

You have just loaded a bundle via its Principal Class as a plugin of an application.

like image 27
Vassilis Avatar answered Sep 30 '22 14:09

Vassilis