我创建了一个插件,并有一些名为 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 的方法。如何从我的基础应用程序实例化和调用这些方法。是否类似于创建对象 ClassOne 并使用该对象调用方法?