【轉(zhuǎn)載】曾夢想仗劍走天涯
1.Xcode IDE概覽
說明:從左到右,依次是“導(dǎo)航窗格(Navigator)->邊列(Gutter)->焦點列(Ribbon)->代碼編輯窗口(Standard/Primary Editor)”。
邊列(Gutter):顯示行號和斷點。
焦點列(Ribbon):灰色深度與代碼嵌套深度相關(guān):鼠標(biāo)懸??赏怀鲲@示右側(cè)相應(yīng)代碼塊(Focus
code blocks on hover),鼠標(biāo)單擊可折疊右側(cè)相應(yīng)代碼塊(Code folding)。
通過“command+,”快捷鍵或”Xcode|Preferences”菜單呼出偏好設(shè)置。
(1)主題及字體(Preferences->Fonts & Colors)
選中一種主題(theme),例如“Midnight”,推薦使用Monokai、Ciapre。將 `*.dvtcolortheme` 文件拷貝到 `~/Library/Developer/Xcode/UserData/FontAndColorThemes/`即可安裝主題, 重啟 Xcode 即可選用。
Xcode默認(rèn)字體為menlo,也可選擇 Consolas、Monaco、Consolas、Droid Sans Mono、Source Code Pro、Bitstream Vera Sans?等其他等寬字體。
shift 選擇主題 Source Editor/Console 中的所有項,點擊 Fonts 可更改字體(大?。?。
(2)編輯器配置(Preferences->Text Editing)
Editing:
Show Line Numbers:在gutter中顯示行號。
Code folding ribbon:顯示焦點列。
Focus code blocks on hover:鼠標(biāo)懸停時突出顯示右側(cè)相應(yīng)代碼塊。
Page guide at column:顯示一行最多支持80個字符的提示分割線。
Indentation:
Prefer indent using:Spaces(為保持一致的視覺呈現(xiàn)和行末注釋對齊,建議使用空格)
Tab width:4 spaces(tab expand,1個tab=4個空格)
Indent width:4 spaces(自動縮進步長=4個空格)
(3)跳轉(zhuǎn)打開方式(Preferences->Navigation)
Activation:When a tab or window opens,make it active。當(dāng)新建窗口或標(biāo)簽時,立即激活聚焦。
Navigation:Uses Primary/Focused Editor。打開Project Navigator中指定的文件時,在主編輯器窗口/當(dāng)前聚焦窗口打開,建議選擇Focused。
Optional Navigation:Uses Single Assistant Editor。打開Project Navigator中指定的文件或跳轉(zhuǎn)到符號定義所在文件(command+單擊)時,若按下option鍵,則在輔助窗口打開。
Double Click Navigation:Uses Separate Tab。雙擊打開Project Navigator中指定的文件或跳轉(zhuǎn)到符號定義所在文件(command+雙擊)時,新建標(biāo)簽頁。
(4)SCM(Preferences->Source Control)
Enable Source Control:啟用/禁用Xcode自帶Source Control Manager(SCM)。
Comparison View:Show local revision in [Right] Side。打開version editor比較窗口時,左側(cè)顯示服務(wù)器基線版本(base),[右側(cè)]顯示本地修改過的工作拷貝(local working copy)。
(5)SDK/Simulator(Preferences->Downloads)
Downloads:可下載Components(SDK&Simulator)和Documentation。
(6)構(gòu)建輸出目錄(Preferences->Locations->Locations)
當(dāng)選擇為Default時,Derived Data的目錄為~/Library/Developer/Xcode/DerivedData。
當(dāng)選擇為Relative時,Derived Data的目錄為當(dāng)前*.xcodeproj所在的目錄。
當(dāng)選擇為Custom時,Derived Data的目錄需自定義。
不建議使用絕對路徑,因為寫死之后,換環(huán)境或換平臺,又要重新修改路徑,且同名project target的build folder會覆蓋,建議使用相對路徑(Relative)
option+點按:查看選中符號的幫助提示(Quick Help for Selected Item)。
option+雙擊:打開選中符號的幫助文檔。
command+0:Show/Hide left tool panel(Navigator Area)
command+1-8:Project/Symbol/Find/Issue/Test/Debug/Breakpoint/Report Navigator
option+command+0:Show/Hide right tool panel(Utility Area)
option+command+1/2:show the file/quick help inspector
可按下command+0隱藏左側(cè)欄Navigator,再按下option+command+0隱藏右側(cè)欄Utility,只顯示Standard/Primary Editor,聚焦有效利用屏幕進行編碼。
control+1:Show Related Items(例如Superclasses/Subclasses、Callers/Callees、ProtocolImplementor/Implemented、Includes/Included By)??奢斎雽崟r搜索匹配。
control+2/3:ShowPrevious/NextHistory。可輸入實時搜索匹配。
control+4:Show Top Level Items。
control+5:Show Group Files(當(dāng)前文件夾內(nèi)的所有文件)。可輸入實時搜索匹配。
control+6:Show DocumentItems(當(dāng)前文件的Symbols)??奢斎雽崟r搜索匹配。
command+shift+[/]:切換標(biāo)簽頁
單/雙指左右滑動(control+command+←/→):在單標(biāo)簽頁打開的多個文件間切換(Go Back/Forward)
command+L:跳轉(zhuǎn)到指定行。
shift+command+O:Open Quickly,快速全局查找文件、符號,非常常用!
command+點擊Editor中選中的符號:跳轉(zhuǎn)到符號定義(jump to definition)。
control+command+J:跳轉(zhuǎn)到指定符號的定義處或?qū)崿F(xiàn)處(Go to Declaration/Definition)。有時工程正在Loading、Indexing或Processing files時,“command+點擊”無法響應(yīng),此時可試試control+command+J。
control+command+↑/↓:切換頭文件/實現(xiàn)文件(switch between a source file (.m,*.mm,*.cc) and the associated header?file(.h),Jump toPrevious/NextCounterpart)。
在Project Navigator中選中文件右鍵或通過菜單“File->Show in Finder”:在Finder中定位該文件。
command+2可聚焦左側(cè)導(dǎo)航欄中的符號導(dǎo)航器。
filter0:底部編輯框輸入符號(show symbols with matching name)= filter1 result+filter3 result
filter1:show only class and protocol symbols (hide other global symbol types),包括project和system層次。
filter2:show only project-defined symbols,過濾顯示當(dāng)前工程中的符號。filter2的結(jié)果是filter1的子集,較常用。
filter3:show only containers(hide members),過濾顯示包含該單詞的符號。
注意:control+6只列出當(dāng)前代碼所在interface的符號,而Symbol Navigator是當(dāng)前工程(Project Scope)的所有符號列表的Hierarchy,符號種類包括Classes/Protocols/Functions/Structs/Unions/Enums/Types/Globals。
編輯器中光標(biāo)所在符號處,Navigate菜單或右鍵快捷菜單可[Reveal?inSymbol Navigator],在符號導(dǎo)航器中定位當(dāng)前符號,亦可查看當(dāng)前符號所屬類的層次。
option+command+←/→:折疊當(dāng)前代碼塊,包括@interface …@end、@implementation …@end
option+shift+command+←/→:折疊該文件內(nèi)所有代碼塊(方法/函數(shù):{Methods&Functions})
control+shift+command+←/→:折疊當(dāng)前注釋塊(/*Comment Blocks*/)
(shift+)option+command+`:MoveFocusto (Previous)Next Area.
command+J:焦點切換(Move Focus),可配合鼠標(biāo)和方向鍵。帶‘+’的“Move focus to a new assistant editor”可以快速在輔助編輯窗口中打開頭文件(*.h)/實現(xiàn)文件(*.m,*.mm)。
shift+command+J:在項目導(dǎo)航中定位當(dāng)前編輯其中打開的文件(Reveal in Project Navigator)。
control+command+N:File | New | Workspace
shift+command+N:File | New | Project
command+X/C/V:剪切/復(fù)制/粘貼
command+Z(+shift):撤銷(重做)/Undo(Redo)
command+[/]:向前/向后縮進(Shift Left/Right)
option+command+[/]:將當(dāng)前光標(biāo)所在行代碼上移/下移一行(Move Line Up/Down)
command+/:以雙斜杠(//)注釋選中的代碼,再此按下可取消
Parentheses/Brackets/Braces Matching:雙擊某個分隔符(如()、[]、{}?等),Xcode會選中匹配代碼塊。
Editor→Structure→Balance Delimiter:根據(jù)當(dāng)前光標(biāo)代碼所在位置,自動向外擴展選擇外層代碼塊。
esc(control+.)就當(dāng)前輸入上下文呼出/隱藏智能提示列表(Auto
CompletionList);
上下方向鍵(↑/↓)在智能提示列表中選擇選項。當(dāng)然,也可以用control+.;enter選中列表中備選的消息符號;
tab在輸入符號不完整時可一截一截匹配;選中消息符號后,tab可在各個參數(shù)占位符之間移動,enter可選中參數(shù)占位符先臨時補位填充。
輸入Objective-C對象及消息名,然后輸入],自動補充對象名左側(cè)的[,完成中綴符(infix natation)包圍。
當(dāng)鼠標(biāo)定位或選中某個符號時,可呼叫右鍵快捷菜單:
Find SelectedTextinWorkspace:在當(dāng)前工作空間查找選中文本或光標(biāo)所在行的OC冒號分割方法名。
Find SelectedSymbolinWorkspace:在當(dāng)前工作空間查找選中文本符號或光標(biāo)所在行的OC冒號分割方法符號。
FindCallHierarchy:查找選中文本符號或光標(biāo)所在行的OC冒號分割方法符號的調(diào)用著(Callers)。
對應(yīng)Find菜單中有Find SelectedTextinProject、Find SelectedSymbolinProject(control+shift+command+F)、FindCallHierarchy(control+shift+command+H)。
說明:Find Call Hierarchy等效于control+1|Callers。
command+F:當(dāng)前文件查找。
shift+command+F(command+3):在Find Navigator中全局查找。可在[Preferences-General]中設(shè)置Find Navigator Detail的顯示行數(shù)(當(dāng)Navigator Area較窄擠壓時)。
Find:可指定查找內(nèi)容(Text/References/Definitions/Regular Expression);
放大鏡:下拉可查看最近查找歷史;
In Project:查找范圍(可指定Group);
Text:匹配規(guī)則(可指定Containing,Matching,Starting with,Ending with);
Case:是否區(qū)分大小寫(可指定 Matching/Ignoring)。
對于查找出來的結(jié)果可以delete刪除非預(yù)期干擾結(jié)果條目,也可以多擇或全選Find Results拷貝出來整理分析。
option+command+F:當(dāng)前文件替換。
option+shift+command+F:在Find Navigator中全局替換。
Replace:逐個替換;
All:所有替換;
Done:替換完成。
例如,光標(biāo)停留在NSMutableArray的insertObject:atIndex的前半截單詞insertObject上時:
有三種復(fù)制方式:
command+C:Copy(光標(biāo)所在位置的單詞):insertObject
control+shift+command+C:Copy Symbol Name(光標(biāo)所在位置的消息符號名稱):-insertObject:atIndex:
option+control+shift+command+C:Copy Qualified Symbol Name(光標(biāo)所在位置的消息符號全名,帶所屬類名):-[NSMutableArray insertObject:atIndex:]
通過后兩種快捷方式,可以便捷地復(fù)制Objective-C特殊的冒號分隔的消息符號名稱。
(7)Open with External Editor——SublimeText
<1>在左側(cè)導(dǎo)航欄(Project Navigator)中選中某個文件右鍵快捷菜單中有【Open with External Editor】,默認(rèn)呼叫Mac OS X自帶的文本編輯器(TextEdit)打開。我們可以按照以下步驟設(shè)置快捷鍵:
【系統(tǒng)偏好設(shè)置(System Preferences)->鍵盤(Keyboard)->快捷鍵(Shortcuts)->應(yīng)用快捷鍵(App Shortcuts)】,
點擊+號,選擇應(yīng)用程序【Xcode.app】
準(zhǔn)確填寫[菜單標(biāo)題],即菜單命令名稱【Open with External Editor】,聚焦[鍵盤快捷鍵]編輯框時,同時按下想要設(shè)置的組合鍵即可,例如option+command+O(??O)。
可針對Xcode工程代碼文件(*.h/*.hh/*.c/*.cc/*.m/*.mm)設(shè)置默認(rèn)打開程序為Sublime Text.app:右鍵Get Info(command+I),Open With選擇Sublime Text.app并且Change All。然后,Xcode|Open with External Editor將在Sublime Text打開選中文件。
<2>另外,可下載安裝OpenInSublimeText插件,支持呼叫SublimeText打開Xcode當(dāng)前正在編輯的文件。
從github下載打開OpenInSublimeText.xcodeproj工程進行編譯(command+B),生成的插件OpenInSublimeText.xcplugin將被集成到Xcode插件目錄(~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins)下。
重啟Xcode將警告提示Unexpected code bundle "OpenInSublimeText.xcplugin",忽略警告選擇[Load Bundle]即可。
Xcode?Editor菜單中將新增[Open In Sublime Text]項,可設(shè)置其快捷鍵位為shift+command+S(??S)。
Assistant Editor有點類似VC中的Code Definition Window,可實現(xiàn)分屏查看代碼Counterpart,方便交叉參考代碼編輯。
option+command+enter/,:打開Assistant Editor。
command+enter:關(guān)閉Assistant Editor。
可通過菜單[View|Assistant Editor]設(shè)置Assistant Editors的方向,例如On Right。
在Mac OS X日常操作中,我們已經(jīng)認(rèn)識到option這把單刀雙擲開關(guān)的妙用:按住鼠標(biāo)移動或三指觸控移動時,按下option鍵可實現(xiàn)豎直塊選;調(diào)節(jié)音量/亮度時,按住option+shift可以四分之一單位微調(diào)。
使用Xcode快捷鍵進行切換或跳轉(zhuǎn)動作時,若同時按下option可以在輔助編輯窗口中打開相應(yīng)文件或符號(For optional navigation (Option-clickingorOption-choosinga file), opens the file in a new Assistant editor pane.)。若在輔助窗口中操作,則在主窗口(Standard Editor)中打開。
option+點擊Project Navigator中選中的文件:在輔助編輯窗口中打開選中文件。
option+command+點擊Editor中選中的符號:在輔助編輯窗口中打開符號定義(jump to definition in assistant editor)。
option+control+command+↑/↓:在輔助窗口中打開對應(yīng)的頭文件(*.h)/實現(xiàn)文件(*.m,*.mm,*.cc)。
點擊查看shift+command+O、shift+command+F(command+3)選中的文件或符號時,可同時按下option在輔助編輯窗口中打開。
在control+1~6中打開選擇結(jié)果時,均可同時按下option在輔助編輯窗口中打開。
在Xcode以上種種切換跳轉(zhuǎn)操作時,按下option的同時按下shift,通常會呼出一個導(dǎo)航窗格(option+command+<),可選擇在new window/tab/assistant-editor打開顯示。
ForOption-Shiftnavigation (Option-Shift-clickor Option-Shift-choosea file), Xcode displays a graphical navigation chooser showing the current layout. The chooserpromptsyou to open the file in anyopeneditor pane in any window and tab, or to open the file in aneweditor pane, window, or tab.
6.環(huán)境變量(Build Setting Macros)
命令行進入HelloWorld工程目錄,執(zhí)行xcodebuild命令并帶上“-showBuildSettings”參數(shù):
$xcodebuild-projectHelloWorld.xcodeproj-targetHelloWorld-configurationDebug-showBuildSettings> xcodebuild_showBuildSettings.txt
則xcodebuild_showBuildSettings.txt中保存了Build settings for action build and target "HelloWorld”,其中dump了所有的環(huán)境變量。
(2)Xcode5(Mac OS X 10.9)的部分環(huán)境變量
約定1:~=當(dāng)前賬戶的HOME目錄,例如“/Users/faner”。
約定2:build構(gòu)建基礎(chǔ)路徑:BUILD_PATH= ~/Library/Developer/Xcode/DerivedData/Build??赏ㄟ^“File->Project Settings”查看Derived Data Location。
約定3:環(huán)境變量宏(Build Setting Macros)引用格式:${MACRO},同Build Phases Run Script中的語法。
下面是摘選自xcodebuild_showBuildSettings.txt的部分常用環(huán)境變量。
ARCHS= i386
CURRENT_ARCH = i386
PLATFORM_DIR = /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform
PLATFORM_NAME = macosx
SDKROOT = /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk
SDK_DIR = /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk
SDK_NAME = macosx10.9
PROJECT = HelloWorld
PROJECT_DIR=~/Projects/Learn Objective-C/HelloWorld
PROJECT_FILE_PATH =${PROJECT_DIR}/HelloWorld.xcodeproj
PROJECT_NAME = HelloWorld
SOURCE_ROOT =${PROJECT_DIR}
SRCROOT =${PROJECT_DIR}
BUILD_DIR=BUILD_PATH/Products
BUILD_ROOT =BUILD_PATH/Products
BUILT_PRODUCTS_DIR =BUILD_PATH/Products/Debug
CONFIGURATION= Debug
CONFIGURATION_BUILD_DIR =BUILD_PATH/Products/Debug
CONFIGURATION_TEMP_DIR =BUILD_PATH/Intermediates/HelloWorld.build/Debug
PRODUCT_NAME = HelloWorld
PRODUCT_TYPE = com.apple.product-type.tool// Project Template: Command Line Tool
TARGET_BUILD_DIR =BUILD_PATH/Products/Debug
TARGET_NAME = HelloWorld
Product -> Edit Scheme(option+command+R)->Arguments->Environment Variables中可以添加自定義環(huán)境變量(Name為名稱,Value為值)。
在Xcode|Build Phases中Add Run Script Build Phase的Run Script將會使用到環(huán)境變量。
一個Target是指在一個Project中構(gòu)建的一個產(chǎn)品,它包含了構(gòu)建該產(chǎn)品的所有文件,以及如何構(gòu)建該產(chǎn)品的配置。
一個定義好構(gòu)建過程的Target成為一個Scheme,可在Scheme中定義Target的六種構(gòu)建過程:Build/Run/Test/Profile/Analyze/Archive。
Run為編譯運行調(diào)試;
Build為只編譯不運行;
Analyze用于進行靜態(tài)代碼分析,可檢測潛在的內(nèi)存泄露(不對稱retain/release 導(dǎo)致的 Potential Leak)或野指針(Use of memory after it is freed)問題。編譯時可選擇 Build Configuration 為 Debug 沿用證書配置;
Profile將調(diào)起 Instruments 工具進行動態(tài)代碼分析,例如使用?Allocations/Leaks 動態(tài)跟蹤分析內(nèi)存泄露。編譯時可選擇 Build Configuration 為 Debug沿用證書配置;
Test用于運行測試,模擬器會啟動并執(zhí)行測試套件;
Archive可以Export as Xcode Archive,然后將 .archive 中的 .app 拖入 itunes可打包生成 ipa 包。
Product ->Edit Scheme(編輯配置,快捷鍵為option+command+R或 shift+command+,/command+<)->Manage Schemes可對Scheme的六種構(gòu)建過程進行配置(可配置項包括Info、Arguments、Options)。
在 Project Navigator 中選中某個 xcodeproj(例如 QQ.xcodeproj),將進入 Project Setting 頁面,可點擊左側(cè)圖標(biāo) show/hide project and targets list:
點擊 targets 項可分別設(shè)置各 target 的 Build Settings;右擊可對 target 進行 delete。
Architectures:Supported Platforms(OS X、iOS),Base SDK,(Valid)Architectures(armv7、arm64、i386)。
Build Active Architecture Only:一般Debug會選擇YES,表示只是編譯連接調(diào)試的目標(biāo)真機對應(yīng)的CPU指令集;對于Release Archive版本,需要選擇NO,這樣編譯出來的安裝包才能同時支持在armv7、arm64機型上安裝。
Build Locations:配置構(gòu)建目錄,包括Intermediate Build Files Path、Build Products Path及其針對Per-Configuration的配置。
Build Options:
Compiler for C/C++/Objective-C默認(rèn)時Apple LLVM 7.0;
Debug Information Format:真機連接調(diào)試時,可斷點定位跟蹤調(diào)試,可選擇DWARF以加快編譯速度;但是真機斷開Xcode運行出現(xiàn)crash時,沒有調(diào)試符號將無法逆向解析定位出問題的代碼符號及其所在具體位置,故一般要選擇DWARF with dSYM File。
此外還包括Bitcode和Testability兩個使能開關(guān)。
Code Signing:包括Entitlements、Identity和Provisioning Profile,詳情參考《iOS Provisioning Profile(Certificate)與Code Signing詳解》。
Deployment:配置發(fā)布及安裝選項。
Linking:
Dead Code Stripping:一般對Debug關(guān)閉,對Release版本開啟以去除無效路徑僵尸代碼,壓縮安裝包體積。
Mangled Names(LINKER_DISPLAYS_MANGLED_NAMES):針對C++ symbols,ld --no-demangle鏈接開關(guān),一般選擇NO。
Mach-O type:Static Library、Dynamic?Library、Executable、Bundle、Relocatable Object File(Position-Dependent )。
Other Linker Flags:例如fobjc-arc(可在Build Rules中針對單個文件配置ARC開關(guān))、-force_load。
Write Link Map File:寫LinkMap文件。
Path to Link Map File:指定鏈接LinkMap文件路徑。
Packaging:
Info.plist File:指定plist文件,對應(yīng)頂部Info標(biāo)簽。
Product Name:為應(yīng)用名稱,例如QQ。
Wrapper Extension:為應(yīng)用擴展,例如app。
Search Paths:
Info.plist File:指定plist文件,對應(yīng)頂部Info標(biāo)簽。
Framework Search Paths: *.framework搜索路徑。
Header?Search Paths:*.h/*.hh頭文件搜索路徑。
Library??Search Paths:靜態(tài)庫、動態(tài)庫搜索路徑。
Apple LLVM 7.0配置:
??Code Generation:
Generate Debug Symbols:YES
Optimization Level:優(yōu)化級別,-Os
??Custom Complier Flags:
??Language
language-Objective-C:Objective-C Automatic Reference Counting,ARC開關(guān)。
??Preprocessing:定義Preprocessor Macros,例如DEBUG、NDEBUG=1。
??Warning Policies:例如可以提高警告級別當(dāng)作錯誤(Treat Warnings as Errors:YES)。
Target Dependencies:設(shè)置依賴target。
Copy Bundle Resources:拷貝的資源文件。
Compile Sources:該target需要編譯的源代碼文件??奢斎胨阉髟创a文件名(xxx.mm),查看或編輯其編譯選項(Compiler Flags),例如 -fobjc-arc表示 ARC(__has_feature(objc_arc))。
Link Binary with Libraries:需要鏈接的庫(*.a、*.framework)。
Embed App Extensions:該APP對應(yīng)的擴展插件。
可以點擊加號,New Run Script Phase,配置custom actions after compiling the Xcode project,相當(dāng)于Visual Studio的Post-builtstep。
當(dāng)然,也可以在Edit Scheme中設(shè)置Pre-actions(custom actions after compiling the Xcode project)、Post-actions(將在編譯鏈接完成后執(zhí)行腳本)。
以下Shell腳本將生成的二進制(Unix executable)文件HelloWorld拷貝到~/Software,然后cd到該目錄下并執(zhí)行HelloWorld:
shift+command+K:清除products|debug或release下的Unix executable文件。
option+shift+command+K:刪除構(gòu)建目錄(Delete/Clean Build Folder),清理Derived Data對應(yīng)target目錄下的Build文件夾(包括intermediate和products)。通常用于重新編譯整個工程,嘗試解決增量編譯時部分符號陳舊導(dǎo)致鏈接不通過的問題。
Product -> Edit Scheme(option+command+R)->Info->Build Configuration:選擇生成版本(Debug or Release)
command + B:構(gòu)建(Buid)
command+8可聚焦左側(cè)導(dǎo)航欄中的日志報告導(dǎo)航器,其中可以查看Build日志。
command+4可定位Issue Navigator:
當(dāng)編譯錯誤(error)和警告(warning)過多時,點擊底端的感嘆號,即可忽略編譯警告,只顯示編譯錯誤:
command + R:運行(Run),可能會重新編譯鏈接。
option+command + R:如果確定代碼沒有改動,加option鍵可免重新編譯鏈接,直接運行上次build成功的product(Run Without Building)。
command + .:停止運行(Stop)。
定義好Target構(gòu)建配置后,接下來需要指定目標(biāo)機編譯。
目標(biāo)機的iPhoneOS.platform必須>=Deployment Target,且Xcode必須支持該機型:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport/目錄下必須含有對應(yīng)iOS系統(tǒng)版本的DeveloperDiskImage.dmg,否則真機編譯會提示Could not find developer disk image。
shift+command+2:可以查看當(dāng)前連接的Device和支持的Simulator。
control+option+command+[/]:Select Previous/Next Destination,在連接多個真機或模擬器之間切換。
shift+command+Y:顯示控制臺(Show/Hide the debug area)
shift+command+C:激活聚焦控制臺,光標(biāo)定位到控制臺呈可輸入狀態(tài)
command+K:清除控制臺(Debug->Debug Workflow->Clear Console)
可安裝BBUDebuggerTuckAway插件,在打字coding時,自動隱藏調(diào)試控制臺
command + \:當(dāng)前行設(shè)置/取消斷點;通過鼠標(biāo)點擊邊列中的藍色斷點來啟用/禁用當(dāng)前行斷點。
command + Y:全局激活或禁用所有的斷點,激活進入調(diào)試模式(此時斷點藍色可見)。
邊列(Gutter)中的斷點/警告可右鍵呼出Reveal in Breakpoint/Issue Navigator。
trick:編輯斷點(Edit Breakpoint):
Condition:設(shè)置斷點的觸發(fā)條件,例如“i==3”(注意不能有空格)表示當(dāng)i等于3時該斷點才會被觸發(fā)。
Ignore:設(shè)置斷點需要被忽略多少次才會中斷,若設(shè)置成5則表示第6次遇到該斷點時才觸發(fā)。
Action:設(shè)置斷點觸發(fā)時的動作,可以為Debugger Command、Log Message、Shell Command或Sound。
例如可設(shè)置以下Debugger Command:
[1]讀取std::string sig的內(nèi)存buffer值:mem read sig.c_str() -c sig.size();
[2]打印NSData實例sig:po sig
F6:下一步(Step Over),逐過程單步調(diào)試,不進入函數(shù)體。
(fn+)F7:進入(Step Into)函數(shù)體??赡芘c多媒體鍵有沖突,故需要fn輔助(建議將功能鍵F1-F12設(shè)置為標(biāo)準(zhǔn)的功能鍵)。
(fn+)F8:跳出(Step Out)函數(shù)體??赡芘c多媒體鍵有沖突,例如呼叫iTunes,故需要fn輔助。
control+command+Y:逐斷點繼續(xù)執(zhí)行(Pause/Continue)。
control+command+C:執(zhí)行到當(dāng)前光標(biāo)所在行(Continue to current line)。
trick:移動指令指針(Move the instruction pointer):
斷點調(diào)試運行時,可以將綠色指針箭頭(Line 47)移動到其他行(Line 49)或其他斷點(Line 51)實現(xiàn)跳轉(zhuǎn)執(zhí)行。
command+8可聚焦左側(cè)導(dǎo)航欄中的日志報告導(dǎo)航器,其中可以查看Debug日志。
shift+command+M:Debug Workflow->View Memory。
Debug Workflow->ShowDisassemblyWhen Debugging,可進行匯編指令級調(diào)試。
trick:修改變量內(nèi)存值(changememory value while debugging):
調(diào)試運行時,可以在底部的調(diào)試窗口(Debug Area,可通過Shift+Command+Y呼出)右鍵某個變量,除了可以進行View Memory/View Value As之外,還可以選擇Edit Value運行時編輯內(nèi)存變量的值。
在調(diào)試驗證某些難以復(fù)現(xiàn)的bug或進行邊界測試時非常有用,可減少每次修改測量樣本值(hardcode)重新編譯鏈接的痛苦。
n/next:step over,等效于F6;
s/step:step into,等效于(fn+)F7;
finish:step out,等效于(fn+)F8;
c/continue:goto next breakpoint,等效于^?Y;
expr/expression:Evaluate a C/ObjC/C++ expression(動態(tài)執(zhí)行C/ObjC/C++表達式);
p/print/expr/expression:print as a?C/C++ basic variable;
// 打印SYSTEM_VERSION(可能要加UIDevice*轉(zhuǎn)換)
(lldb)p[[[UIDevicecurrentDevice]systemVersion] doubleValue]
po/expr-O/expression-O:Print as an Objective-C object;
// 打印屏幕bounds(可能要加UIScreen*轉(zhuǎn)換)
(lldb)poNSStringFromCGRect([[UIScreenmainScreen] bounds])
// 打印狀態(tài)欄frame(可能要加UIApplication*轉(zhuǎn)換)
(lldb)poNSStringFromCGRect([UIApplicationsharedApplication].statusBarFrame)
// 打印當(dāng)前keyWindow的根視圖(可能要加UIViewController*轉(zhuǎn)換)
(lldb)po[[[UIApplicationsharedApplication] keyWindow] rootViewController]
call:調(diào)用。其實上述p/po后接表達式(expression)也有調(diào)用的功能,一般只在不需要顯式輸出,或是無返回值時使用call,用于動態(tài)調(diào)試插入調(diào)用代碼。
例如可以在viewDidLoad:里面設(shè)置斷點,然后在程序中斷的時候輸入以下命令:
// 調(diào)用后,繼續(xù)運行程序,view的背景色將變成紅色
(lldb)call[self.viewsetBackgroundColor:[UIColorredColor]]
bt(backtrace),打印當(dāng)前調(diào)用堆棧(crash堆棧),“btall”可打印所有thread的堆棧(相當(dāng)于command+6的Debug Session Navigation)。
image:可用于尋址,有多個組合命令,比較實用的一種用法是尋找棧地址對應(yīng)的代碼(行)位置。
例如某個UITableView總共有2個section,當(dāng)其引用的currentSection.index≥2時將會引起[UITableView rectForHeaderInSection:]調(diào)用異常,可使用expr動態(tài)改值制造crash場景模擬調(diào)試。
此時crash時的控制臺bt顯示異常出現(xiàn)在應(yīng)用層代碼“0x00d055b8-[FACategoryTableView FACategorySectionHeaderDidTouched:] +744”處(其中0x00d055b8為當(dāng)前棧(代碼段)偏移量,744為棧幀偏移量——PC指針相對函數(shù)入口的偏移)。
那么具體是FACategoryTableView.m文件哪一行代碼調(diào)用引起的異常呢?此時通過“image lookup --address”后接bt的call stack中的代碼段偏移地址(0x00d055b8)即可定位出異常調(diào)用的代碼行位置。
x/memoryread:dump指定地址的內(nèi)存(Read from the memory of the process being debugged),后接起止地址或-c指定count加起始地址。可help mem read查看幫助:
Syntax:
memory read []
Command Options Usage:
size指定內(nèi)存塊(block/chunk)的大小
--size ):The size in bytes to use when displaying with the selected format.
count指定內(nèi)存塊(block/item)的個數(shù),可配合起始地址使用。
-c ( --count ):The number of total items to display.
format指定內(nèi)容顯示格式,格式符同print:c-char,s-string,d-decimal,x-hex。
-f ( --format ):Specify a format to be used for display.
Command Samples:
(a)起止地址,以下基于起始地址偏移量指定截至地址。
(lldb)mem read 0x10b88f0c 0x10b88f0c+9
0x10b88f0c: 39 38 37 36 35 34 33 32 31 ? ? ? ? ? ? ? ? ? ? ? 987654321
(b)可在起始地址后使用-c指定需要dump的字節(jié)數(shù),以上等效:
(lldb)mem read 0x10b88f0c -c 9
0x10b88f0c: 39 38 37 36 35 34 33 32 31 ? ? ? ? ? ? ? ? ? ? ? 987654321
(c)起始地址+內(nèi)存塊size+內(nèi)存塊count(dump hex format)
(lldb)memory read -s 1 -f x -c 9 0x10b88f0c
0x10b88f0c: 0x39 0x38 0x37 0x36 0x35 0x34 0x33 0x32
0x10b88f14: 0x31
說明:dump的memory chunk為1byte,以上總共dump了chunk size*chunk count=9byte。
(d)起始地址+內(nèi)存塊size+內(nèi)存塊count(dump char format)
(lldb)memory read -s 1 -f c -c 9 0x10b88f0c
0x10b88f0c: 987654321
(e)起始地址+內(nèi)存塊size+內(nèi)存塊count(dump string format)
(lldb)mem read 0x10b5cf2c -f s -c 1
0x10b88f0c: "987654321"
(f)起始地址+內(nèi)存塊size+內(nèi)存塊count(dump int format)
(lldb)memory read -s 4 -f x -c 3 0x10b88f0c
0x10b88f0c: 0x36373839 0x32333435 0x109f0031
說明:以上指定chunk尺寸為4byte(-s ?4),chunk數(shù)量為3,共dump了12個byte。
memorywrite:改寫指定地址的內(nèi)存(Write to the memory of the process being debugged)。可自行help mem write查看幫助:
Syntax: memory write [ [...]]
trick:lldb打印無效問題
在使用LLDB調(diào)試命令p/po打印C類型(包括復(fù)合類型)或Objective-C對象時,可能會遇到屬性不存在或類型不匹配的問題。
例1——斷點調(diào)試,打印當(dāng)前UIViewController的frame:
由于Xcode lldb本身的bug,對屬性的點引用有時會無法識別,例如執(zhí)行(lldb)p self.view.frame報錯:property 'frame' not found on object of type 'UIView *'
將對屬性的點引用改為對屬性的getter調(diào)用,執(zhí)行(lldb)p [self.view frame]依舊報錯:no known method '-frame'; cast the message send to the method's return type
由于Xcode lldb本身的bug,對返回的復(fù)合類型也無法直接識別,此時可采用顯示類型轉(zhuǎn)換,執(zhí)行(lldb)p (CGRect)[self.view frame]不會報錯!
例2——斷點調(diào)試,打印當(dāng)前UIViewController的navigationController堆棧和childViewControllers數(shù)組:
點引用報錯寫法(property not found):(lldb)po self.navigationController.viewControllers
調(diào)用getter正確打?。?b>(lldb)po [[self navigationController] viewControllers]
點引用報錯寫法(property not found):(lldb)po self.childViewControllers
調(diào)用getter正確打印:(lldb)po [self childViewControllers]
(6)啟用NSZombieEnabled調(diào)試EXC_BAD_ACCESS
當(dāng)你對已釋放的對象發(fā)送消息(90%的可能是對引用計數(shù)為0的對象再release)或release那些autorelease對象時,就會出現(xiàn)報EXC_BAD_ACCESS這樣的錯誤。
默認(rèn)設(shè)置下 Xcode不會給你定位具體是哪一行代碼不該去使用已釋放的對象,或者release用錯了。
Product -> Edit Scheme(option+command+R)-> Diagnostics,勾選“Objective-C”之后的“Enable Zombie Objects”。
設(shè)置NSZombieEnabled環(huán)境變量后,一個對象銷毀時會被轉(zhuǎn)化為_NSZombie;設(shè)置NSZombieEnabled后,當(dāng)你向一個已經(jīng)釋放的對象發(fā)送消息,這個對象就不只是報EXC_BAD_ACCESS Crash,還會放出一個錯誤消息,然后以一種可預(yù)測的可以產(chǎn)生debug斷點的方式消失, 因此我們可以找到具體或者大概是哪個對象被錯誤的釋放或引用了。
注意:NSZombieEnabled只能在調(diào)試的時候使用,千萬不要忘記在產(chǎn)品發(fā)布的時候去掉,因為NSZombieEnabled不會真正去釋放dealloc對象的內(nèi)存,一直開啟后果自負!
參考
《Xcode Overview-About Xcode》《Xcode Keyboard Shortcuts and Gestures》
《Xcode Key Bindings & Gestures》《提升Xcode效率的小技巧》
《用Vim編輯器輔助Xcode》《Vim命令圖解和xVim使用》《升級Xcode7后XVim插件失效的修復(fù)辦法》
《Xcode的文件組織》《Xcode設(shè)置命令行啟動參數(shù)》
《Xcode環(huán)境變量及路徑設(shè)置》《Xcode構(gòu)建輸出目錄》
《從VC到Xcode》《workspace & subProject & target》
《Xcode創(chuàng)建靜態(tài)庫》《Xcode4制作靜態(tài)庫1》《Xcode4制作靜態(tài)庫2》
《Xcode4.3下制作framework》《Xcode添加依賴的靜態(tài)庫工程》
《Building static libraries with the iPhone SDK》《Static Libraries and Cross-Project References》
《iOS開發(fā)之統(tǒng)計Xcode代碼行數(shù)》《Start Developing iOS Apps Today》
《使用Xcode創(chuàng)建Hello World項目》《使用Xcode5和Interface Builder創(chuàng)建Hello World App》
《LLDB Quick Start Guide》《LLDB to GDB Command Map》《Xcode gdb/lldb調(diào)試命令》
《Xcode LLDB Debug教程》《LLDB調(diào)試命令初探》《iOS應(yīng)用崩潰日志揭秘》《經(jīng)營你的iOS應(yīng)用日志》
《讓lldb提升你的效率》《iOS/OSX 調(diào)試:跳舞吧!與LLDB共舞華爾茲》《NSLog效率低下的原因及嘗試lldb斷點打印Log》