开发者

principal class of NSBundle

Plz help me to understand what is principa开发者_如何学JAVAlClass 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.


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.


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.


The principalClass allows you to know what class to start using once you have loaded a bundle. For example, say you are using bundles to represent plugins for an image processing app. When you tell the Objective-C runtime to load the bundle "CSISharpener.bundle", it will load a bunch of new classes into memory. However, you still need to know the name of the class to send messages to in order to actually use the plugin.

In our example, principalClass might return CSISharpeningFilter, which is conforms to the plugin protocol we told the plugin developers to use. So we can create an instance of "principalClass" and start using it, without knowing ahead of time what the class name is.

In other words, principalClass is there to allow the programs that load bundles and easy way to find an "entry point" into the code they have just loaded. Exactly what it is used for is going to depend on what code is loading the bundle and what it is using it for.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜