了解Xcode

0.认识Xcode
《了解Xcode》

1.主题及字体

“command+,”呼叫出偏好设置(首选项),选择“Fonts & Colors”,选中一种主题(theme),例如“Midnight”,然后shift选择Source Editor/Console中的所有项,点击Fonts设置字体。Xcode默认字体为menlo,可选其他等宽字体Consolas/Monaco。

2.View and Navigate

View Navigator

command+0:Show/Hide left tool panel

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

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

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)过多时,只显示编译错误:
《了解Xcode》
点击底端的感叹号,即可只显示编译错误,忽略编译警告:**
《了解Xcode》

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》

    原文作者:B0B0
    原文地址: https://segmentfault.com/a/1190000000603438
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞