前言
一般可以將編程語言分為兩種,編譯語言和直譯式語言。
像C++,Objective C都是編譯語言。編譯語言在執(zhí)行的時(shí)候,必須先通過編譯器生成機(jī)器碼,機(jī)器碼可以直接在CPU上執(zhí)行,所以執(zhí)行效率較高。
像JavaScript,Python都是直譯式語言。直譯式語言不需要經(jīng)過編譯的過程,而是在執(zhí)行的時(shí)候通過一個(gè)中間的解釋器將代碼解釋為CPU可以執(zhí)行的代碼。所以,較編譯語言來說,直譯式語言效率低一些,但是編寫的更靈活,也就是為啥JS大法好。
iOS開發(fā)目前的常用語言是:Objective和Swift。二者都是編譯語言,換句話說都是需要編譯才能執(zhí)行的。二者的編譯都是依賴于Clang + LLVM. 篇幅限制,本文只關(guān)注Objective C,因?yàn)樵砩洗笸‘悺?/p>
可能會有同學(xué)想問,我不懂編譯的過程,寫代碼也沒問題啊?這點(diǎn)我是不否定的。但是,充分理解了編譯的過程,會對你的開發(fā)大有幫助。本文的最后,會以以下幾個(gè)例子,來講解如何合理利用XCode和編譯
**attribute**
Clang警告處理
預(yù)處理
插入編譯期腳本
提高項(xiàng)目編譯速度
對于不想看我啰里八嗦講一大堆原理的同學(xué),可以直接跳到本文的最后一個(gè)章節(jié)。
iOS編譯
Objective C采用Clang(swift采用swift)作為編譯器前端,LLVM(Low level vritual machine)作為編譯器后端。
簡單的編譯過程如圖
編譯器前端
編譯器前端的任務(wù)是進(jìn)行:語法分析,語義分析,生成中間代碼(intermediate representation )。在這個(gè)過程中,會進(jìn)行類型檢查,如果發(fā)現(xiàn)錯(cuò)誤或者警告會標(biāo)注出來在哪一行。
編譯器后端
編譯器后端會進(jìn)行機(jī)器無關(guān)的代碼優(yōu)化,生成機(jī)器語言,并且進(jìn)行機(jī)器相關(guān)的代碼優(yōu)化。iOS的編譯過程,后端的處理如下
- LVVM優(yōu)化器會進(jìn)行BitCode的生成,鏈接期優(yōu)化等等。
LLVM機(jī)器碼生成器會針對不同的架構(gòu),比如arm64等生成不同的機(jī)器碼。
執(zhí)行一次XCode build的流程
當(dāng)你在XCode中,選擇build的時(shí)候(快捷鍵command+B),會執(zhí)行如下過程
編譯信息寫入輔助文件,創(chuàng)建編譯后的文件架構(gòu)(name.app)
處理文件打包信息,例如在debug環(huán)境下
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: inherit; font-size: 17px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; line-height: inherit;">
Entitlements:
{
"application-identifier" = "app的bundleid";
"aps-environment" = development;
}
</pre>
執(zhí)行CocoaPod編譯前腳本
例如對于使用CocoaPod的工程會執(zhí)行CheckPods Manifest.lock
編譯各個(gè).m文件,使用CompileC和clang命令。
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: inherit; font-size: 17px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; line-height: inherit;">
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-size: 15px; color: rgb(62, 62, 62); line-height: inherit; caret-color: rgb(62, 62, 62); text-align: start;">
CompileC ClassName.o ClassName.m normal x86_64 objective-c com.apple.compilers.llvm.clang.1_0.compiler
export LANG=en_US.US-ASCII
export PATH="..."
clang -x objective-c -arch x86_64 -fmessage-length=0 -fobjc-arc... -Wno-missing-field-initializers ... -DDEBUG=1 ... -isysroot iPhoneSimulator10.1.sdk -fasm-blocks ... -I 上文提到的文件 -F 所需要的Framework -iquote 所需要的Framework ... -c ClassName.c -o ClassName.c
</pre>
</pre>
通過這個(gè)編譯的命令,我們可以看到
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: inherit; font-size: 17px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; line-height: inherit;">
clang是實(shí)際的編譯命令
-x objective-c 指定了編譯的語言
-arch x86_64制定了編譯的架構(gòu),類似還有arm7等
-fobjc-arc 一些列-f開頭的,指定了采用arc等信息。這個(gè)也就是為什么你可以對單獨(dú)的一個(gè).m文件采用非ARC編程。
-Wno-missing-field-initializers 一系列以-W開頭的,指的是編譯的警告選項(xiàng),通過這些你可以定制化編譯選項(xiàng)
-DDEBUG=1 一些列-D開頭的,指的是預(yù)編譯宏,通過這些宏可以實(shí)現(xiàn)條件編譯
-iPhoneSimulator10.1.sdk 制定了編譯采用的iOS SDK版本
-I 把編譯信息寫入指定的輔助文件
-F 鏈接所需要的Framework
-c ClassName.c 編譯文件
-o ClassName.o 編譯產(chǎn)物
</pre>
鏈接需要的Framework,例如 Foundation.framework,AFNetworking.framework,ALiPay.fframework
編譯xib文件
拷貝xib,圖片等資源文件到結(jié)果目錄
編譯ImageAssets
處理info.plist
執(zhí)行CocoaPod腳本
拷貝Swift標(biāo)準(zhǔn)庫
創(chuàng)建.app文件和對其簽名
IPA包的內(nèi)容
例如,我們通過iTunes Store下載微信,然后獲得ipa安裝包,然后實(shí)際看看其安裝包的內(nèi)容。
右鍵ipa,重命名為.zip
雙擊zip文件,解壓縮后會得到一個(gè)文件夾。所以,ipa包就是一個(gè)普通的壓縮包。
- 右鍵圖中的
WeChat
,選擇顯示包內(nèi)容,然后就能夠看到實(shí)際的ipa包內(nèi)容了。
二進(jìn)制文件的內(nèi)容
通過XCode的Link Map File,我們可以窺探二進(jìn)制文件中布局。在XCode -> Build Settings -> 搜索map -> 開啟Write Link Map File
開啟后,在編譯,我們可以在對應(yīng)的Debug/Release目錄下看到對應(yīng)的link map的text文件。默認(rèn)的目錄在
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: inherit; font-size: 17px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; line-height: inherit;">
~/Library/Developer/Xcode/DerivedData/<TARGET-NAME>-對應(yīng)ID/Build/Intermediates/<TARGET-NAME>.build/Debug-iphoneos/<TARGET-NAME>.build/
</pre>
例如,我的TargetName是EPlusPan4Phone,目錄如下
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: inherit; font-size: 17px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; line-height: inherit;">
/Users/huangwenchen/Library/Developer/Xcode/DerivedData/EPlusPan4Phone-eznmxzawtlhpmadnbyhafnpqpizo/Build/Intermediates/EPlusPan4Phone.build/Debug-iphonesimulator/EPlusPan4Phone.build
</pre>
這個(gè)映射文件的主要包含以下部分:
Object files
這個(gè)部分包括的內(nèi)容
.o 文文件,也就是上文提到的.m文件編譯后的結(jié)果。
.a文件
需要link的framework
!Arch: x86_64 #Object files: [0] linker synthesized [1] /EPlusPan4Phone.build/EPlusPan4Phone.app.xcent [2]/EPlusPan4Phone.build/Objects-normal/x86_64/ULWBigResponseButton.o … [1175]/UMSocial_Sdk_4.4/libUMSocial_Sdk_4.4.a(UMSocialJob.o) [1188]/iPhoneSimulator10.1.sdk/System/Library/Frameworks//Foundation.framework/Foundation
這個(gè)區(qū)域的存儲內(nèi)容比較簡單:前面是文件的編號,后面是文件的路徑。文件的編號在后續(xù)會用到
Sections
這個(gè)區(qū)域提供了各個(gè)段(Segment)和節(jié)(Section)在可執(zhí)行文件中的位置和大小。這個(gè)區(qū)域完整的描述克可執(zhí)行文件中的全部內(nèi)容。
其中,段分為兩種
__TEXT 代碼段
__DATA 數(shù)據(jù)段
例如,之前寫的一個(gè)App,Sections區(qū)域如下,可以看到,代碼段的
__text節(jié)的地址是0x1000021B0,大小是0x0077EBC3,而二者相加的下一個(gè)位置正好是__stubs的位置0x100780D74。
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: inherit; font-size: 17px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; line-height: inherit;">
Sections:
位置 大小 段 節(jié)
Address Size Segment Section
0x1000021B0 0x0077EBC3 __TEXT __text //代碼
0x100780D74 0x00000FD8 __TEXT __stubs
0x100781D4C 0x00001A50 __TEXT __stub_helper
0x1007837A0 0x0001AD78 __TEXT __const //常量
0x10079E518 0x00041EF7 __TEXT __objc_methname //OC 方法名
0x1007E040F 0x00006E34 __TEXT __objc_classname //OC 類名
0x1007E7243 0x00010498 __TEXT __objc_methtype //OC 方法類型
0x1007F76DC 0x0000E760 __TEXT __gcc_except_tab
0x100805E40 0x00071693 __TEXT __cstring //字符串
0x1008774D4 0x00004A9A __TEXT __ustring
0x10087BF6E 0x00000149 __TEXT __entitlements
0x10087C0B8 0x0000D56C __TEXT __unwind_info
0x100889628 0x000129C0 __TEXT __eh_frame
0x10089C000 0x00000010 __DATA __nl_symbol_ptr
0x10089C010 0x000012C8 __DATA __got
0x10089D2D8 0x00001520 __DATA __la_symbol_ptr
0x10089E7F8 0x00000038 __DATA __mod_init_func
0x10089E840 0x0003E140 __DATA __const //常量
0x1008DC980 0x0002D840 __DATA __cfstring
0x10090A1C0 0x000022D8 __DATA __objc_classlist // OC 方法列表
0x10090C498 0x00000010 __DATA __objc_nlclslist
0x10090C4A8 0x00000218 __DATA __objc_catlist
0x10090C6C0 0x00000008 __DATA __objc_nlcatlist
0x10090C6C8 0x00000510 __DATA __objc_protolist // OC協(xié)議列表
0x10090CBD8 0x00000008 __DATA __objc_imageinfo
0x10090CBE0 0x00129280 __DATA __objc_const // OC 常量
0x100A35E60 0x00010908 __DATA __objc_selrefs
0x100A46768 0x00000038 __DATA __objc_protorefs
0x100A467A0 0x000020E8 __DATA __objc_classrefs
0x100A48888 0x000019C0 __DATA __objc_superrefs // OC 父類引用
0x100A4A248 0x0000A500 __DATA __objc_ivar // OC iar
0x100A54748 0x00015CC0 __DATA __objc_data
0x100A6A420 0x00007A30 __DATA __data
0x100A71E60 0x0005AF70 __DATA __bss
0x100ACCDE0 0x00053A4C __DATA __common
</pre>
Symbols
Section部分將二進(jìn)制文件進(jìn)行了一級劃分。而,Symbols對Section中的各個(gè)段進(jìn)行了二級劃分, 例如,對于__TEXT __text,表示代碼段中的代碼內(nèi)容。
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: inherit; font-size: 17px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; line-height: inherit;">
0x1000021B0 0x0077EBC3 __TEXT __text //代碼
</pre>
而對應(yīng)的Symbols,起始地址也是0x1000021B0 。其中,文件編號和上文的編號對應(yīng)
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: inherit; font-size: 17px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; line-height: inherit;">
[2]/EPlusPan4Phone.build/Objects-normal/x86_64/ULWBigResponseButton.o
</pre>
具體內(nèi)容如下
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: inherit; font-size: 17px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; line-height: inherit;">
Symbols:
地址 大小 文件編號 方法名
Address Size File Name
0x1000021B0 0x00000109 [ 2] -[ULWBigResponseButton pointInside:withEvent:]
0x1000022C0 0x00000080 [ 3] -[ULWCategoryController liveAPI]
0x100002340 0x00000080 [ 3] -[ULWCategoryController categories]
....
</pre>
到這里,我們知道OC的方法是如何存儲的,我們再來看看ivar是如何存儲的。首先找到數(shù)據(jù)棧中__DATA __objc_ivar
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: inherit; font-size: 17px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; line-height: inherit;">
0x100A4A248 0x0000A500 __DATA __objc_ivar
</pre>
然后,搜索這個(gè)地址0x100A4A248,就能找到ivar的存儲區(qū)域。
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: inherit; font-size: 17px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; line-height: inherit;">
0x100A4A248 0x00000008 [ 3] OBJC_IVAR$_ULWCategoryController._liveAPI
</pre>
值得一提的是,對于String,會顯式的存儲到數(shù)據(jù)段中,例如,
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: inherit; font-size: 17px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; line-height: inherit;">
0x1008065C2 0x00000029 [ 11] literal string: http://sns.whalecloud.com/sina2/callback
</pre>
所以,若果你的加密Key以明文的形式寫在文件里,是一件很危險(xiǎn)的事情。
dSYM 文件
我們在每次編譯過后,都會生成一個(gè)dsym文件。dsym文件中,存儲了16進(jìn)制的函數(shù)地址映射。
在App實(shí)際執(zhí)行的二進(jìn)制文件中,是通過地址來調(diào)用方法的。在App crash的時(shí)候,第三方工具(Fabric,友盟等)會幫我們抓到崩潰的調(diào)用棧,調(diào)用棧里會包含crash地址的調(diào)用信息。然后,通過dSYM文件,我們就可以由地址映射到具體的函數(shù)位置。
XCode中,選擇Window -> Organizer可以看到我們生成的archier文件
然后,
右鍵 -> 在finder中顯示。
右鍵 -> 查看包內(nèi)容。
關(guān)于如何用dsym文件來分析崩潰位置,可以查看我之前的一篇博客。
- iOS 如何調(diào)試第三方統(tǒng)計(jì)到的崩潰報(bào)告[1]
那些你想到和想不到的應(yīng)用場景
****attribute****
或多或少,你都會在第三方庫或者iOS的頭文件中,見到過attribute。比如
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: inherit; font-size: 17px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; line-height: inherit;">
attribute ((warn_unused_result)) //如果沒有使用返回值,編譯的時(shí)候給出警告
</pre>
__attribtue__
是一個(gè)高級的的編譯器指令,它允許開發(fā)者指定更更多的編譯檢查和一些高級的編譯期優(yōu)化。
分為三種:
函數(shù)屬性 (Function Attribute)
類型屬性 (Variable Attribute )
變量屬性 (Type Attribute )
語法結(jié)構(gòu)
__attribute__
語法格式為:attribute((attribute-list)) 放在聲明分號“;”前面。
比如,在三方庫中最常見的,聲明一個(gè)屬性或者方法在當(dāng)前版本棄用了
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; font-size: 15px; color: rgb(62, 62, 62); line-height: inherit; caret-color: rgb(62, 62, 62); text-align: start;">
@property (strong,nonatomic)CLASSNAME * property __deprecated;
</pre>
這樣的好處是:給開發(fā)者一個(gè)過渡的版本,讓開發(fā)者知道這個(gè)屬性被棄用了,應(yīng)當(dāng)使用最新的API,但是被__deprecated的屬性仍然可以正常使用。如果直接棄用,會導(dǎo)致開發(fā)者在更新Pod的時(shí)候,代碼無法運(yùn)行了。
attribtue的使用場景很多,本文只列舉iOS開發(fā)中常用的幾個(gè):
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; font-size: 15px; color: rgb(62, 62, 62); line-height: inherit; caret-color: rgb(62, 62, 62); text-align: start;">
//棄用API,用作API更新
define __deprecated attribute((deprecated))
//帶描述信息的棄用
define __deprecated_msg(_msg) attribute((deprecated(_msg)))
//遇到__unavailable的變量/方法,編譯器直接拋出Error
define __unavailable attribute((unavailable))
//告訴編譯器,即使這個(gè)變量/方法 沒被使用,也不要拋出警告
define __unused attribute((unused))
//和__unused相反
define __used attribute((used))
//如果不使用方法的返回值,進(jìn)行警告
define __result_use_check attribute((warn_unused_result))
//OC方法在Swift中不可用
define __swift_unavailable(_msg) attribute((availability(swift, unavailable, message=_msg)))
</pre>
Clang警告處理
你一定還見過如下代碼:
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: inherit; font-size: 17px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; line-height: inherit;">
pragma clang diagnostic push
pragma clang diagnostic ignored "-Wundeclared-selector"
///代碼
pragma clang diagnostic pop
</pre>
這段代碼的作用是
對當(dāng)前編譯環(huán)境進(jìn)行壓棧
忽略-Wundeclared-selector(未聲明的)Selector警告
編譯代碼
對編譯環(huán)境進(jìn)行出棧
通過clang diagnostic push/pop,你可以靈活的控制代碼塊的編譯選項(xiàng)。
我在之前的一篇文章里,詳細(xì)的介紹了XCode的警告相關(guān)內(nèi)容。本文篇幅限制,就不詳細(xì)講解了。
- iOS 合理利用Clang警告來提高代碼質(zhì)量[2]
在這個(gè)鏈接,你可以找到所有的Clang warnings警告
- fuckingclangwarnings
預(yù)處理
所謂預(yù)處理,就是在編譯之前的處理。預(yù)處理能夠讓你定義編譯器變量,實(shí)現(xiàn)條件編譯。比如,這樣的代碼很常見
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: inherit; font-size: 17px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; line-height: inherit;">
ifdef DEBUG
//...
else
//...
endif
</pre>
同樣,我們同樣也可以定義其他預(yù)處理變量,在XCode-選中Target-build settings中,搜索proprecess。然后點(diǎn)擊圖中藍(lán)色的加號,可以分別為debug和release兩種模式設(shè)置預(yù)處理宏。比如我們加上:TestServer,表示在這個(gè)宏中的代碼運(yùn)行在測試服務(wù)器
然后,配合多個(gè)Target(右鍵Target,選擇Duplicate),單獨(dú)一個(gè)Target負(fù)責(zé)測試服務(wù)器。這樣我們就不用每次切換測試服務(wù)器都要修改代碼了。
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: inherit; font-size: 17px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; line-height: inherit;">
ifdef TESTMODE
//測試服務(wù)器相關(guān)的代碼
else
//生產(chǎn)服務(wù)器相關(guān)代碼
endif
</pre>
插入腳本
通常,如果你使用CocoaPod來管理三方庫,那么你的Build Phase是這樣子的:
其中:[CP]開頭的,就是CocoaPod插入的腳本。
Check Pods Manifest.lock,用來檢查cocoapod管理的三方庫是否需要更新
Embed Pods Framework,運(yùn)行腳本來鏈接三方庫的靜態(tài)/動(dòng)態(tài)庫
Copy Pods Resources,運(yùn)行腳本來拷貝三方庫的資源文件
而這些配置信息都存儲在這個(gè)文件(.xcodeprog)里
到這里,CocoaPod的原理也就大致搞清楚了,通過修改xcodeproject,然后配置編譯期腳本,來保證三方庫能夠正確的編譯連接。
同樣,我們也可以插入自己的腳本,來做一些額外的事情。比如,每次進(jìn)行archive的時(shí)候,我們都必須手動(dòng)調(diào)整target的build版本,如果一不小心,就會忘記。這個(gè)過程,我們可以通過插入腳本自動(dòng)化。
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: inherit; font-size: 17px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; line-height: inherit;">
buildNumber={PROJECT_DIR}/
((
buildNumber" "
{INFOPLIST_FILE}"
</pre>
這段腳本其實(shí)很簡單,讀取當(dāng)前pist的build版本號,然后對其加一,重新寫入。
使用起來也很簡單:
Xcode - 選中Target - 選中build phase
選擇添加Run Script Phase
然后把這段腳本拷貝進(jìn)去,并且勾選Run Script Only When installing,保證只有我們在安裝到設(shè)備上的時(shí)候,才會執(zhí)行這段腳本。重命名腳本的名字為Auto Increase build number
然后,拖動(dòng)這個(gè)腳本的到Link Binary With Libraries下面
腳本編譯打包
腳本化編譯打包對于CI(持續(xù)集成)來說,十分有用。iOS開發(fā)中,編譯打包必備的兩個(gè)命令是:
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: inherit; font-size: 17px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; line-height: inherit;">
//編譯成.app
xcodebuild -workspace projectName -configuration
buildAppToDir
//打包
xcrun -sdk iphoneos PackageApplication -v projectName.app -o
ipaName.ipa
通過info命令,可以查看到詳細(xì)的文檔
info xcodebuild
</pre>
完整的腳本[3],使用的時(shí)候,需要拷貝到工程的根目錄
提高項(xiàng)目編譯速度
通常,當(dāng)項(xiàng)目很大,源代碼和三方庫引入很多的時(shí)候,我們會發(fā)現(xiàn)編譯的速度很慢。在了解了XCode的編譯過程后,我們可以從以下角度來優(yōu)化編譯速度:
查看編譯時(shí)間
我們需要一個(gè)途徑,能夠看到編譯的時(shí)間,這樣才能有個(gè)對比,知道我們的優(yōu)化究竟有沒有效果。對于XCode 8,關(guān)閉XCode,終端輸入以下指令
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: inherit; font-size: 17px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; line-height: inherit;">
$ defaults write com.apple.dt.Xcode ShowBuildOperationDuration YES
</pre>
然后,重啟XCode,然后編譯,你會在這里看到編譯時(shí)間。
代碼層面的優(yōu)化
forward declaration
所謂forward declaration,就是@class CLASSNAME,而不是#import CLASSNAME.h。這樣,編譯器能大大提高#import的替換速度。
對常用的工具類進(jìn)行打包(Framework/.a)
打包成Framework或者靜態(tài)庫,這樣編譯的時(shí)候這部分代碼就不需要重新編譯了。
常用頭文件放到預(yù)編譯文件里
XCode的pch文件是預(yù)編譯文件,這里的內(nèi)容在執(zhí)行XCode build之前就已經(jīng)被預(yù)編譯,并且引入到每一個(gè).m文件里了。
編譯器選項(xiàng)優(yōu)化
Debug模式下,不生成dsym文件
上文提到了,dysm文件里存儲了調(diào)試信息,在Debug模式下,我們可以借助XCode和LLDB進(jìn)行調(diào)試。所以,不需要生成額外的dsym文件來降低編譯速度。
Debug開啟Build Active Architecture Only
在XCode -> Build Settings -> Build Active Architecture Only 改為YES。這樣做,可以只編譯當(dāng)前的版本,比如arm7/arm64等等,記得只開啟Debug模式。這個(gè)選項(xiàng)在高版本的XCode中自動(dòng)開啟了。
Debug模式下,關(guān)閉編譯器優(yōu)化
編譯器優(yōu)化
參考
[1]http://blog.csdn.net/hello_hwc/article/details/50036323
[2]http://blog.csdn.net/Hello_Hwc/article/details/46425503
[3]https://github.com/LeoMobileDeveloper/Blogs/blob/master/DemoProjects/Scripts/autoIPA.sh