0

我正在用可可编写一个应用程序,它在运行时安装额外的 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?

谢谢

4

1 回答 1

1

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

如果你正在尝试寻找插件,那么只需枚举[[NSBundle mainBundle] builtInPlugInsPath]你自己的内容。

我认为基本问题是查找和加载插件的每一步都失败了,但是您没有检查任何假设,因此您没有意识到这一点。最后,当你去获取一个图标时,你得到nil的是捆绑包而不是注意到。当然,当您向它询问其图标时,您正在发送消息nilnil返回。

于 2012-06-02T10:10:57.687 回答