2012-06-02 47 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