gpt4 book ai didi

cocoa - 在运行时加载的 NSBundle 不提供资源路径

转载 作者:行者123 更新时间:2023-12-03 17:57:43 29 4
gpt4 key购买 nike

我正在使用 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?

谢谢

最佳答案

-[NSBundle pathsForResourcesOfType:inDirectory:] 不采用任意目录名称。它采用 bundle 资源目录的子目录的名称。

如果您想查找插件,那么只需自己枚举 [[NSBundle mainBundle]builtInPlugInsPath] 的内容即可。

我认为基本问题是查找和加载插件的每一步都失败了,但你没有检查你的任何假设,所以你没有意识到这一点。最后,当你去获取一个图标时,你会得到 bundle 的 nil 并且没有注意到。当然,当您向它询问其图标时,您是在发送 nil 消息并得到 nil 回复。

关于cocoa - 在运行时加载的 NSBundle 不提供资源路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10861349/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com