2012-06-02 31 views
0

我正在编程一个可可的应用程序,它在运行时安装额外的NSBundles。但我无法获得任何资源。这是到目前为止的代码:在运行时加载的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"]]; 
} 

为什么都可以从已经已运行时返回零为他们的图标中已经安装的图标和插件程序的启动插件?

感谢

回答

1

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

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

我认为最基本的问题是每一个查找和加载插件的步骤都失败了,但是你没有检查任何你的假设,所以你没有意识到这一点。最后,当你去获得一个图标时,你会得到nil这个包,而不会注意到。当然,当你问它的图标时,你正在传送nil并获得nil