OC靜態(tài)庫里NSClassFromString得到nil的解決

如果你在靜態(tài)庫中有從類名反射回類的代碼, 如下:

NSString *myClassStr = @"myClass";  
Class myClazz = NSClassFromString(myClassStr);  
if (myClazz) {  
    id myClassInit = [[myClazz alloc] init];  
}

有時候(經(jīng)常)會出現(xiàn)得到了Class為nil的情況, 網(wǎng)上搜索, 一般是這么說的:

The class object named by aClassName, or nil if no class by that name is currently loaded. If aClassName is nil, returns nil.

來自于64位系統(tǒng)的一個bug:

IMPORTANT: For 64-bit and iPhone OS applications, there is a linker bug that prevents -ObjC from loading objects files from static libraries that contain only categories and no classes. The workaround is to use the -all_load or -force_load flags. -all_load forces the linker to load all object files from every archive it sees, even those without Objective-C code. -force_load is available in Xcode 3.2 and later. It allows finer grain control of archive loading. Each -force_load option must be followed by a path to an archive, and every object file in that archive will be loaded.

就我的實測

  • 首先, 你需要在你的主項目(的target)對build setting進行更改, 而不是靜態(tài)庫的項目!
  • 其次, -all_load有效, -force_load甚至編譯都過不了
  • 最后, 結合上面, 就是在主項目(引用靜態(tài)庫的項目)的build setting里面搜索other linker flags, 然后把-all_load加進去就行了
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容