首先,這恰恰是沒有什麼與Xcode。
現在,您不能動態加載靜態庫,因爲靜態庫只是一個目標文件的集合,而這些目標文件本身並不是可執行的。
爲了加載動態庫,使用dlopen()
API:
void *handle = dlopen("/path/to/library.dylib", RTLD_LAZY);
爲了得到一個C函數指針:
int (*computeAnswer)(void) = dlsym(handle, "ComputeAnswer");
int answer = computeAnswer(); // 42
爲了得到一個C++函數指針,未extern "C"
聯動裝置(重整名稱):
int (*mangledFunction)(void) = dlsym(handle, "[email protected]");
你甚至可以破解自己在連接編譯器的Objective-C的命名約定:
@class MyShinyClass;
Class cls = dlsym(handle, "OBJC_CLASS_$_MyShinyClass");
MyShinyClass *instance = [[cls alloc] init];
當你與庫完成後,處理它:
dlclose(handle);
什麼你有一個NSBundle嘗試,爲什麼它一個「不好的解決方案」?而關於dlopen(),你已經在回答http://stackoverflow.com/a/21338282/1187415中提到你以前的類似問題? –
順便說一句,而不是重複你的問題http://stackoverflow.com/questions/21325604/how-to-load-dll-file-which-returns-assembly-in-objective-c並忽略給定的答案,你可以嘗試如果答案沒有解決您的問題,可以改進問題或發表評論。 –
嗨...我想.a(靜態庫)文件在NSBundle中不受支持。我用這個... Class exampleClass; id newInstance; NSString * str = @「/ Users/Desktop/libCDT.ChunkUtil.a」; NSBundle * bundleToLoad = [NSBundle bundleWithPath:str]; if(exampleClass == [bundleToLoad classNamed:@「IReaderClass」])newInstance = [[exampleClass alloc] init]; // [newInstance doSomething]; }但bundleToLoad對象包含空值...那麼該怎麼辦? – user3200854