我正在用可可编写一个应用程序,它在运行时安装额外的 NSBundle。但我无法从中获取任何资源。这是到目前为止的代码:
-(void)load {
NSString *appSupportSubpath = [[NSBundle mainBundle] builtInPlugInsPath];
NSArray *bundlePaths = [NSBundle pathsForResourcesOfType:@"bundle" inDirectory:appSupportSubpath];
NSEnumerator *searchPathEnum;
NSString *currPath;
searchPathEnum = [bundlePaths objectEnumerator];
NSMutableArray *classes = [[NSMutableArray alloc] init];
while(currPath = [searchPathEnum nextObject])
{
NSBundle *pluginBundle = [NSBundle bundleWithPath:currPath];
if(![pluginBundle isLoaded]) {
[pluginBundle load];
}
Class principalClass = [pluginBundle principalClass];
if ([principalClass isSubclassOfClass:[AddOn class]]) {
[classes addObject:principalClass];
}
}
addOnLibrary = classes;
}
-(NSArray *)infos {
NSMutableArray *infos = [[NSMutableArray alloc] init];
NSEnumerator *enumerator;
Class theClass;
enumerator = [addOnLibrary objectEnumerator];
while(theClass = [enumerator nextObject])
{
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setValue:[theClass patchname] forKey:@"name"];
[dictionary setValue:NSStringFromClass(theClass) forKey:@"classname"];
[dictionary setValue:[theClass icon] forKey:@"icon"];
//Here icon is nil for AddOns added during runtime
[infos addObject:dictionary];
}
return infos;
}
//the addon-icon method
+(NSImage *)icon {
NSBundle *myBundle = [NSBundle bundleForClass:[self class]];
return [[NSImage alloc] initWithContentsOfFile:[myBundle pathForImageResource:@"icon.png"]];
}
为什么程序启动时可用的插件有图标,而在运行时安装的插件的图标返回 nil?
谢谢