在iOS開發中,其實workspace的使用沒有徹底發揮出來,最近作了一些研究,也想把以前寫過的代碼整理下,由於iOS裏面的佈局方式,交互方式也就那麼幾種。因此,整理好了以後,更能快捷開發,並且可以造成積累。因此把經常使用的東西封裝成lib文件。git
我本身的實施過程以下,同時會穿插一些本身參考的文章的連接或者方法。主要參考連接:github
一、新建一個文件夾,而後新建一個workspace, 將這個workspace放置在這個文件夾中,建立workspace的過程與建立工程相似。xcode
以下是建立的截圖:app
二、在這個workspace中建立一個static lib 工程佈局
三、在workspace中建立一個empty projectui
四、工程建立完成,後面就有不少事情要作了,配置工程,empty project可以使用 static lib中的代碼。this
首先,在empty project 中的Build Phases->Link Binary with Libraries 中添加 libTTStyles.a spa
而後在 empty project 的Build Settings->User Header Search Paths 裏面添加"$(BUILT_PRODUCTS_DIR)」 和「$(BUILT_PRODUCTS_DIR)/static_library_name」兩個選項,才能在編譯的時候搜索到相關的頭文件code
而後,在static lib工程的 Other Linker Flags 中添加-ObjC, -all_load, -force_load 這三個選項,至少添加前兩個,否則會報錯,
這個是Xcode裏面的一個bug, Three20裏面有這麼一段代碼:
1 #define TT_FIX_CATEGORY_BUG(name) @interface TT_FIX_CATEGORY_BUG_##name @end \ 2 @implementation TT_FIX_CATEGORY_BUG_##name @end
就是爲了解決,static lib中的category沒法引用 的問題,添加了一個同名的空類。上面最開始的連接裏面有這個問題的解答:
The 「User Header Search Paths」 setting defines the headers available as quoted imports (eg 「#import 「MyLibraryClass.h」) while the 「Header Search Paths」 setting defines those headers available as bracketed imports (eg 「#import ). I’ve found that Xcode will only autocomplete header names in the quoted form so I always add libraries to the user header search path even though, from my project’s perspective, they might be more appropriate as system level (angle bracketed) libraries. When using a static library which includes categories we will also have to add the 「-ObjC」 flag to the 「Other Linker Flags」 build setting. This will force the linker to load all objective-c classes and categories from the library. If the library contains only categories 「-all_load」 or 「-force_load」 may be needed as well. See Technical Q&A QA1490 for a more detailed explanation of these settings.
五、配置工程的編譯選項,控制編譯工程之間的依賴關係。
在,TTProject的 scheme manage裏面添加以前創建的lib文件,編譯TTProjects的時候,就會先編譯 statib lib的內容,而後再編譯自身工程
At this point Xcode should have detected this implicit dependency between our app’s project and the static library’s project and have automatically configured our schemes correctly. Unfortunately I haven’t found this to be the case in practice. Instead we will have to edit our current scheme and add the static library’s build target before our app’s build target.
總結:用這種方式來建立工程,最大的好處就是,每次每一個項目的開發,都能有所積累,到後面,能夠達到3--5天便可開發一個App的地步。
後面會把這套lib庫開源出來,也是使用了不少第三方的代碼,本身作了一些封裝和吸取吧。加TT前綴的緣由是,Three20那套裏面有不少東西都不錯。借鑑了一些。可是不像three20那樣龐大,不重複造車吧。。