瞭解Xcode

0.認識Xcode
請輸入圖片描述express


1.主題及字體macos

「command+,」呼叫出偏好設置(首選項),選擇「Fonts & Colors」,選中一種主題(theme),例如「Midnight」,而後shift選擇Source Editor/Console中的全部項,點擊Fonts設置字體。Xcode默認字體爲menlo,可選其餘等寬字體Consolas/Monaco。segmentfault


2.View and Navigatexcode

View Navigatorsass

command+0:Show/Hide left tool panelapp

command+1-8:Project/Symbol/Find/Issue/Test/Debug/Breakpoint/Log Navigator編輯器

option+command+0:Show/Hide right tool panelide

option+command+1:show the file inspector函數

option+command+2:show quick help inspector字體

View Editor Organization

control+1:Show Related Items(例如Superclasses/Subclasses、Callers/Callees、Protocol Implementor/Implemented、Includes/Included By)。可輸入實時搜索匹配。

control+2/3:Show Previous/Next History。可輸入實時搜索匹配。
control+4:Show Top Level Items
control+5:Show Group Files(當前文件夾內的全部文件)。可輸入實時搜索匹配。

control+6:Show Document Items(當前文件的Symbols)。可輸入實時搜索匹配。

可直接選中符號,而後「Navigate->Reveal in Symbol Navigator」,打開該接口的符號列表。

command+J:焦點切換(Move Focus),可配合鼠標和方向鍵。帶‘+’的「Move focus to a new assistant editor」能夠快速在輔助編輯窗口中打開頭文件(.h)/實現文件(.m,*.mm)。

shift+command+J:在項目導航中定位當前文件(Reveal in Project Navigator)。


3.基本操做

**control+command+N:File | New | Workspace
shift+command+N:File | New | Project
command+[/]:向前/向後縮進
option+command+[/]:Move Line Up/Down
command+/:註釋選中的代碼
command+shift+[/]:切換標籤頁
單指左右滑動(control+command+←/→):在單標籤頁打開的多個文件間切換
option+command+←/→:摺疊當前代碼塊

option+shift+command+←/→:摺疊該文件內全部代碼塊(方法/函數)
shift+command+Y:顯示控制檯(Show/Hide the debug area)
option+command+R:編輯配置(Edit Scheme)
括號匹配:雙擊某個分隔符(如{}、()、[] 等),Xcode會選中匹配代碼塊。
焦點列:灰色深度與代碼嵌套深度相關,鼠標懸停可突出顯示右側相應代碼塊,鼠標單擊可摺疊右側相應代碼塊(Code Folding)。
說明:從左到右,依次是「導航窗格(Navigator)->邊列(Gutter)->焦點列(Focus
Ribbon)->代碼編輯窗口(Standard Editor)」。
編譯錯誤(error)和警告(warning)過多時,只顯示編譯錯誤:
請輸入圖片描述
點擊底端的感嘆號,便可只顯示編譯錯誤,忽略編譯警告:**
請輸入圖片描述


4. 符號定位跳轉

control+command+↑/↓:切換頭文件/實現文件(switch between a source file (.m,.mm,.cc) and the associated header (.h) file)。
shift+command+O:Open Quickly,可快速查找「文件」、符號」。

command+點擊Editor中選中的符號:跳轉到符號定義(jump to definition)。

control+command+J:跳轉到指定符號的定義處或實現處(Go to Declaration/Definition)。

右鍵或菜單File->Show in Finder:在Finder中定位該文件


5. 查找/替換

command+F:當前文件查找。

option+command+F:當前文件替換。

shift+command+F(command+3):全局查找。也能夠選擇符號後,control+點擊->Find Selected Text in workspace...

option+shift+command+F:全局替換。


6.輔助編輯窗口(Assistant Editor)

Assistant Editor有點相似VC中的Code Definition Window。
option+command+enter:打開Assistant Editor。

command+enter:關閉Assistant Editor。

使用快捷鍵進行切換或跳轉動做時,若同時按下option能夠在輔助編輯窗口中打開相應文件或符號(For optional navigation (Option-clicking or Option-choosing a 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+↑/↓:在輔助窗口中打開對應的頭文件(.h)/實現文件(.m,.mm,.cc)。

點擊查看shift+command+O、shift+command+F(command+3)選中的文件或符號時,可同時按下option在輔助編輯窗口中打開。

在control+1~6中打開選擇結果時,都可同時按下option在Assistant Editor中打開。

若在按下option的同時按下shift一般會出現一個導航窗格,可選擇在new window/tab/assistant-editor顯示打開。

For Option-Shift navigation (Option-Shift-click or Option-Shift-choose a file), Xcode displays a graphical navigation chooser showing the current layout. The chooserprompts you to open the file in anyopen editor pane in any window and tab, or to open the file in anew editor pane, window, or tab.


7.代碼自動完成功能

esc就當前輸入上下文呼出/隱藏Auto Completion提示;上下方向鍵在提示中選擇選項,enter(return)選中,tab可一截一截匹配;tab可在各個佔位符之間移動。

輸入Objective-C對象及 ],自動完成中綴符(infix natation)包圍。


8.幫助文檔(Documentation and API References Help)

option+點按:查看選中符號的幫助提示(Quick Help for Selected Item)。
option+雙擊:打開選中符號的幫助文檔。


9.環境變量(Build Setting Macros)

(1)查看環境變量宏

命令行進入HelloWorld工程目錄,執行xcodebuild命令並帶上「-showBuildSettings」參數:

iMac-Faner:HelloWorld faner$ xcodebuild -project HelloWorld.xcodeproj -target HelloWorld -configuration Debug -showBuildSettings > xcodebuild_showBuildSettings.txt

則xcodebuild_showBuildSettings.txt中保存了Build settings for action build and target "HelloWorld」:,其中dump了全部的環境變量。

(2)Xcode5(macosx10.9)的部分環境變量

約定1:~=當前帳戶的HOME目錄,例如「/Users/faner」。

約定2:build構建基礎路徑:BUILD_PATH = ~/Library/Developer/Xcode/DerivedData/Build。可經過「File->Project Settings」查看Derived Data Location。

約定3:環境變量宏(Build Setting Macros)引用格式:${MACRO},同Build Phases Run Script中的語法。

下面是摘選自xcodebuild_showBuildSettings.txt的部分經常使用環境變量。

(a) ARCH & PLATFORM & SDK

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

(b) PROJECT & SOURCE

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}

(c) BUILD & CONFIGURATION

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

(d) PRODUCT & TARGET

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


10.運行調試

shift + command + K:清除(Clean)

command + B:構建(Buid)

command + R:運行(Run),可能會先編譯。若按下control直接運行上次build的product(Run Without Building)。

command + .:中止運行(Stop)

command + \:當前行設置/取消斷點;經過鼠標點擊藍色斷點來啓用/禁用當前行斷點。

command + Y:全局激活或禁用全部的斷點,激活進入調試模式(此時斷點藍色可見)。

編輯斷點(Edit Breakpoint):

Condition:設置斷點的觸發條件,例如「i==3」(注意不能有空格)表示當i等於3時該斷點纔會被觸發。

Ignore:設置斷點須要被忽略多少次纔會中斷,若設置成5則表示第6次遇到該斷點時才觸發。

Action:設置斷點觸發時的動做,能夠爲Debugger Command、Log Message、Shell Command或Sound。

例如可設置如下Debugger Command:

(1)讀取std::string sig的內存buffer值:mem read sig.c_str() -c sig.size();

(2)打印NSData實例sig:po sig

F6:下一步(Step Over),逐過程單步調試,不進入函數體。

(fn+)F7:進入(Step Into)函數體。可能與多媒體鍵有衝突,故須要fn輔助。

(fn+)F8:跳出(Step Out)函數體。可能與多媒體鍵有衝突,例如呼叫iTunes,故須要fn輔助。

control+command+Y:逐斷點(continue)繼續執行。

shift+command+M:Debug Workflow->View Memory。

command+K:Debug Workflow->Clear Console。

Debug Workflow->ShowDisassembly When Debugging,可進行彙編指令級調試。

lldb調試命令:

(1)n/next:step over;

(2)s/step:step into;

(3)finish:step out;

(4)c/continue:goto next breakpoint;

(5)p: print;

(6)expression:後接表達式,同p。

(7)memory read:-- Read from the memory of the process being debugged.

dump指定地址的內存,後接起止地址或-c指定count加起始地址。可help mem read查看幫助:

Syntax:

memory read [ ]

Command Options Usage:

size指定內存塊(block/item)的大小,默認爲1byte。

--size <byte-size> ):The size in bytes to use when displaying with the selected format.

count指定內存塊(block/item)的個數,可配合起始地址使用。

-c <count> ( --count <count> ):The number of total items to display.

format指定內容顯示格式,格式符同print:c-char,s-string,d-decimal,x-hex。

-f <format> ( --format <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)起始地址+內存塊count

(lldb)mem read 0x10b88f0c -c 9

0x10b88f0c: 39 38 37 36 35 34 33 32 31 987654321

(c)起始地址+內存塊size+內存塊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

(d)起始地址+內存塊size+內存塊count(dump char format)

(lldb)memory read -s 1 -f c -c 9 0x10b88f0c

0x10b88f0c: 987654321

(e)起始地址+內存塊size+內存塊count(dump string format)

(lldb)mem read 0x10b5cf2c -f s -c 1

0x10b88f0c: "987654321"

(f)起始地址+內存塊size+內存塊count(dump int format)

(lldb)memory read -s 4 -f x -c 3 0x10b88f0c

0x10b88f0c: 0x36373839 0x32333435 0x109f0031

(8)memory write:-- Write to the memory of the process being debugged.

改寫指定地址的內存,可help mem write查看幫助。

Syntax: memory write

<

address> [ [...]]

11.設置NSZombieEnabled調試EXC_BAD_ACCESS

當你對已釋放的對象發送消息(90%的多是對引用計數爲0的對象再release)或release那些autorelease對象時,就會出現報EXC_BAD_ACCESS這樣的錯誤。
默認設置下 Xcode不會給你定位具體是哪一行代碼不應去使用已釋放的對象,或者release用錯了。
Product -> EditScheme(option+command+R) -> Diagnostics ,勾選「Objective-C」以後的「Enable Zombie Objects」。
設置NSZombieEnabled環境變量後,一個對象銷燬時會被轉化爲_NSZombie;設置NSZombieEnabled後,當你向一個已經釋放的對象發送消息,這個對象就不僅是報EXC_BAD_ACCESS Crash,還會放出一個錯誤消息,而後以一種可預測的能夠產生debug斷點的方式消失, 所以咱們能夠找到具體或者大概是哪一個對象被錯誤的釋放或引用了。
注意:NSZombieEnabled只能在調試的時候使用,千萬不要忘記在產品發佈的時候去掉,由於NSZombieEnabled不會真正去釋放dealloc對象的內存,一直開啓後果自負!


參考

《Xcode Overview-About Xcode》

《Xcode Keyboard Shortcuts and Gestures》

《Xcode插件》《用Vim編輯器輔助Xcode》《Vim命令圖解和xVim使用》

《Xcode的文件組織》《Xcode設置命令行啓動參數》

《Xcode環境變量及路徑設置》《Xcode構建輸出目錄》

《從VC到Xcode》《workspace & subProject & target》

《Xcode建立靜態庫》《Xcode4製做靜態庫 1》《Xcode4製做靜態庫 2》

《Xcode4.3下製做framework》《Xcode添加依賴的靜態庫工程》

《Building static libraries with the iPhone SDK》《Static Libraries and Cross-Project References》

《Start Developing iOS Apps Today》

《使用Xcode建立Hello World項目》

《使用Xcode5和Interface Builder建立Hello World App》

《LLDB Quick Start Guide》

《LLDB to GDB Command Map》

相關文章
相關標籤/搜索