我创建了一个插件,并有一些名为PluginPrincipalClass,ClassOne和ClassTwo的3个类。我的类中有以下代码片段。
#import <Cocoa/Cocoa.h>
@interface PluginPrincipalClass : NSObject
@end
#import "PluginPrincipalClass.h"
@implementation PluginPrincipalClass
- (NSInteger)getDownloadPercentage
{
NSLog(@"getDownloadPercentage");
return 10;
}
- (void)downloadSoftwareUpdate
{
NSLog(@"downloadSoftwareUpdate");
}
@end
#import <Cocoa/Cocoa.h>
@interface ClassOne : NSObject
@end
#import "ClassOneh"
@implementation ClassOne
- (void)ClassOneMethod
{
NSLog(@"ClassOneMethod");
}
@end
#import <Cocoa/Cocoa.h>
@interface ClassTwo : NSObject
@end
#import "ClassTwo.h"
@implementation ClassTwo
- (void)ClassTwoMethod
{
NSLog(@"ClassTwoMethod");
}
@end
在我的BaseApplication中加载插件并调用主要类我有以下代码片段
NSString *zStrPlugInsPath = [[NSBundle mainBundle] builtInPlugInsPath];
NSArray *zAryBundlePaths = [NSBundle pathsForResourcesOfType:@"plugin"
inDirectory:zStrPlugInsPath];
NSString * zStrPathToPlugin = [zAryBundlePaths lastObject];
NSBundle *znsBundlePlugin = [NSBundle bundleWithPath:zStrPathToPlugin];
// instantiate the principal class and call the method
Class zPrincipalClass = [znsBundlePlugin principalClass];
id zPrincipalClassObj = [[zPrincipalClass alloc]init];
NSInteger downloadPer = [zPrincipalClassObj getDownloadPercentage];
NSLog(@"downloadPer = %ld",downloadPer);
[zPrincipalClassObj downloadSoftwareUpdate];
这很好。如果我想调用ClassOne或ClassTwo.How的方法来实例化并从我的基础应用程序中调用这些方法。它是否类似于创建对象ClassOne并使用该对象调用方法?
答案 0 :(得分:1)
我(如果我理解你的问题),你想使用NSBundle的classNamed:
方法:)
像这样:
Class zSecondaryClass = [znsBundlePlugin classNamed: @"StudentClass"];