我正在使用cocoa编写应用程序,在运行时安装其他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时已安装?
由于
答案 0 :(得分:1)
-[NSBundle pathsForResourcesOfType:inDirectory:]
不接受任意目录名称。它采用bundle的Resources目录的子目录名称。
如果您正在尝试查找插件,那么只需自己枚举[[NSBundle mainBundle] builtInPlugInsPath]
的内容。
我认为基本问题是查找和加载插件的每一步都失败了,但是你没有检查过任何假设,所以你没有意识到这一点。最后,当你去获取一个图标时,你得到了nil
的捆绑而没有注意到。当然,当你问它的图标时,你会发送消息nil
并回复nil
。