0

実行時に追加の NSBundles をインストールするココアを使用してアプリをプログラミングしています。しかし、私はそれからリソースを取得できません。これまでのコードは次のとおりです。

-(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:]任意のディレクトリ名を取りません。バンドルの Resources ディレクトリのサブディレクトリの名前を取ります。

プラグインを見つけようとしている場合は、自分のコンテンツを列挙して[[NSBundle mainBundle] builtInPlugInsPath]ください。

基本的な問題は、プラグインを見つけてロードするすべてのステップが失敗したことだと思いますが、前提を確認していないため、それに気づいていませんでした。最後に、アイコンを取得しようとするnilと、バンドルを取得していることに気づきません。もちろん、アイコンを要求すると、メッセージを送っnilnil戻ってきます。

于 2012-06-02T10:10:57.687 に答える