Xcode基本操作

Posted 小怡情ifelse

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Xcode基本操作相关的知识,希望对你有一定的参考价值。

这应该是Xcode5的样貌,经过两个版本6和7的冲击,应该会变变化很多,不过本质是相差无几,学习挺好,有时间整理一份最新的Xcode操作文章。

1.Xcode IDE概览



说明:从左到右,依次是“导航窗格(Navigator)->边列(Gutter)->焦点列(Ribbon)->代码编辑窗口(Standard/Primary Editor)”。

  • 边列(Gutter:显示行号和断点。
  • 焦点列(Ribbon):灰色深度与代码嵌套深度相关:鼠标悬停可突出显示右侧相应代码块(Focus code blocks on hover),鼠标单击可折叠右侧相应代码块(Code folding)。

2.偏好设置

通过“command+,”快捷键或”Xcode|Preferences”菜单呼出偏好设置。

(1)主题及字体(Preferences->Fonts & Colors)

选中一种主题theme),例如“Midnight”,然后shift选择Source Editor/Console中的所有项,点击Fonts设置字体。Xcode默认字体为menlo,可选Consolas、Monaco等其他等宽字体。

(2)编辑器配置(Preferences->Text Editing)

Editing

  • Show Line Numbers:在gutter中显示行号。
  • Code folding ribbon:显示焦点列。

    Focus code blocks on hover:鼠标悬停时突出显示右侧相应代码块。

  • Page guide at column:显示一行最多支持80个字符的提示分割线。
Indentation
  • Prefer indent using:Spaces(为保持一致的视觉呈现和行末注释对齐,建议使用空格)
  • Tab width:4 spaces(tab expand,1个tab=4个空格)
  • Indent width:4 spaces(自动缩进步长=4个空格)

(3)跳转打开方式Preferences->Navigation

  • Activation:When a tab or window opens,make it active。当新建窗口或标签时,立即激活聚焦。
  • Navigation:Uses Primary/Focused Editor。打开Project Navigator中指定的文件时,在主编辑器窗口/当前聚焦窗口打开,建议选择Focused
  • Optional Navigation:Uses Single Assistant Editor。打开Project Navigator中指定的文件或跳转到符号定义所在文件(command+单击)时,若按下option键,则在辅助窗口打开。
  • Double Click Navigation:Uses Separate Tab。双击打开Project Navigator中指定的文件或跳转到符号定义所在文件(command+双击)时,新建标签页

(4)SCM(Preferences->Source Control)

  • Enable Source Control:启用/禁用Xcode自带Source Control Manager(SCM)。
  • Comparison View:Show local revision in [Right] Side。打开version editor比较窗口时,左侧显示服务器基线版本(base),[右侧]显示本地修改过的工作拷贝(local working copy)。

(5)SDK/SimulatorPreferences->Downloads

Downloads:可下载Components(SDK&Simulator)和Documentation。

(6)构建输出目录(Preferences->Locations->Locations)

  • 当选择为Default时,Derived Data的目录为~/Library/Developer/Xcode/DerivedData。
  • 当选择为Relative时,Derived Data的目录为当前*.xcodeproj所在的目录。
  • 当选择为Custom时,Derived Data的目录需自定义。
不建议使用绝对路径,因为写死之后,换环境或换平台,又要重新修改路径,且同名project target的build folder会覆盖,建议使用相对路径(Relative

3.代码阅读

(1)Help

option+点按:查看选中符号的帮助提示(Quick Help for Selected Item)。

option+双击:打开选中符号的帮助文档。

(2)View Navigator

command+0:Show/Hide left tool panel(Navigator Area)

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

option+command+0Show/Hide right tool panel(Utility Area)

option+command+1/2:show the file/quick help inspector

可按下command+0隐藏左侧栏Navigator,再按下option+command+0隐藏右侧栏Utility,只显示Standard/Primary Editor,聚焦有效利用屏幕进行编码。

(3)View Editor Organization

control+1:Show Related Items(例如Superclasses/SubclassesCallers/CalleesProtocol 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)。可输入实时搜索匹配。

(4)Eidtor Window/Tab Switch

command+shift+[/]:切换标签页

单/双指左右滑动(control+command+←/→):在单标签页打开的多个文件间切换(Go Back/Forward)

(5)Symbol Jump

command+L跳转到指定行。

shift+command+O:Open Quickly,快速全局查找文件、符号,非常常用!

command+点击Editor中选中的符号:跳转到符号定义(jump to definition)。

control+command+J:跳转到指定符号的定义处或实现处(Go to Declaration/Definition)。有时工程正在Loading、Indexing或Processing files时,“command+点击”无法响应,此时可试试control+command+J

control+command+↑/↓:切换头文件/实现文件(switch between a source file (.m,*.mm,*.cc) and the associated header file(.h),Jump to Previous/Next Counterpart)。

在Project Navigator中选中文件右键或通过菜单“File->Show in Finder”:在Finder中定位该文件。

(6)Symbol Navigator

command+2可聚焦左侧导航栏中的符号导航器。

  • 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,过滤显示当前工程中的符号。filter2的结果是filter1的子集,较常用。
  • filter3:show only containers(hide members),过滤显示包含该单词的符号。
注意control+6只列出当前代码所在interface的符号,而Symbol Navigator是当前工程(Project Scope)的所有符号列表的Hierarchy,符号种类包括 Classes Protocols Functions Structs Unions Enums Types Globals
编辑器中光标所在符号处,Navigate菜单或右键快捷菜单可[Reveal in Symbol Navigator],在符号导航器中定位当前符号,亦可查看当前符号所属类的层次。

(7)Code Folding

option+command+←/→:折叠当前代码块,包括 @interface …@end @implementation …@end

option+shift+command+←/→:折叠该文件内所有代码块(方法/函数: Methods&Functions 

control+shift+command+←/→:折叠当前注释块(/*Comment Blocks*/

(8)Focus Switch

(shift+)option+command+`:MoveFocus to (Previous)Next Area.

command+J:焦点切换(Move Focus),可配合鼠标和方向键。带‘+’的“Move focus to a new assistant editor”可以快速在辅助编辑窗口中打开头文件(*.h)/实现文件(*.m,*.mm)。

shift+command+J:在项目导航中定位当前编辑其中打开的文件(Reveal in Project Navigator)。

4.代码编辑

(1)File | New

control+command+N:File | New | Workspace

shift+command+N:File | New | Project

(2)Text Editing

command+X/C/V:剪切/复制/粘贴

command+Z(+shift):撤销(重做)/Undo(Redo)

command+[/]:向前/向后缩进(Shift Left/Right)

option+command+[/]:将当前光标所在行代码上移/下移一行(Move Line Up/Down)

command+/:以双斜杠(//)注释选中的代码,再此按下可取消

Parentheses/Brackets/Braces Matching:双击某个分隔符(如()、[]、 等),Xcode会选中匹配代码块

Editor→Structure→Balance Delimiter:根据当前光标代码所在位置,自动向外扩展选择外层代码块。

(3)Auto Completion

esccontrol+.)就当前输入上下文呼出/隐藏智能提示列表(Auto Completion List);

上下方向键↑/↓)在智能提示列表中选择选项。当然,也可以用control+.;enter选中列表中备选的消息符号;

tab在輸入符号不完整时可一截一截匹配;选中消息符号后,tab可在各个参数占位符之间移动enter可选中参数占位符先临时补位填充。

输入Objective-C对象及消息名,然后输入 ],自动补充对象名左侧的,完成中缀符(infix natation)包围。

(4)Find菜单(control+单击)

当鼠标定位或选中某个符号时,可呼叫右键快捷菜单
  • Find Selected Text in Workspace:在当前工作空间查找选中文本或光标所在行的OC冒号分割方法名。
  • Find Selected Symbol in Workspace:在当前工作空间查找选中文本符号或光标所在行的OC冒号分割方法符号。
  • Find Call Hierarchy:查找选中文本符号或光标所在行的OC冒号分割方法符号的调用着(Callers)。
对应Find菜单中有Find Selected Text in Project、Find Selected Symbol in Projectcontrol+shift+command+F)、Find Call Hierarchy(control+shift+command+H
说明:Find Call Hierarchy等效于control+1|Callers。

(5)Find & Replace

command+F:当前文件查找。

shift+command+F(command+3):在Find Navigator中全局查找。可在[Preferences-General]中设置Find Navigator Detail的显示行数(当Navigator Area较窄挤压时)。

  • Find:可指定查找内容(Text/References/Definitions/Regular Expression);
  • 放大镜:下拉可查看最近查找历史;
  • In Project:查找范围(可指定Group);
  • Text:匹配规则(可指定Containing,Matching,Starting with,Ending with);
  • Case:是否区分大小写(可指定 Matching/Ignoring)。
对于查找出来的结果可以delete删除非预期干扰结果条目,也可以多择或全选Find Results拷贝出来整理分析。 option+command+F :当前文件替换。

option+shift+command+F:在Find Navigator中全局替换。

  • Replace:逐个替换;
  • All:所有替换;
  • Done:替换完成。

(6)Copy Symbol

例如,光标停留在NSMutableArray的insertObject:atIndex的前半截单词insertObject上时:
有三种复制方式:
  • command+CCopy(光标所在位置的单词):insertObject
  • control+shift+command+CCopy Symbol Name(光标所在位置的消息符号名称):-insertObject:atIndex:
  • option+control+shift+command+CCopy Qualified Symbol Name(光标所在位置的消息符号全名,带所属类名):-[NSMutableArray insertObject:atIndex:]
通过后两种快捷方式,可以便捷地复制Objective-C特殊的冒号分隔的消息符号名称。

(7)Open with External Editor——SublimeText

<1>在左侧导航栏(Project Navigator)中选中某个文件右键快捷菜单中有【Open with External Editor】,默认呼叫Mac OS X自带的文本编辑器(TextEdit)打开。我们可以按照以下步骤设置快捷键:
  • 【系统偏好设置(System Preferences)->键盘(Keyboard)->快捷键(Shortcuts)->应用快捷键(App Shortcuts)】,
  • 点击+号,选择应用程序【Xcode.app】
  • 准确填写[菜单标题],即菜单命令名称【Open with External Editor】,聚焦[键盘快捷键]编辑框时,同时按下想要设置的组合键即可,例如option+command+O(⌥⌘O)。
可针对Xcode工程代码文件(*.h/*.hh/*.c/*.cc/*.m/*.mm)设置默认打开程序为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当前正在编辑的文件。
  • 从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]项,可设置其快捷键位为shift+command+S(⇧⌘S)。

5.辅助编辑(Assistant Editor)

(1)打开/关闭辅助编辑窗口

Assistant Editor有点类似VC中的Code Definition Window,可实现分屏查看代码Counterpart,方便交叉参考代码编辑。

  • option+command+enter/,打开Assistant Editor。
  • command+enter关闭Assistant Editor。

可通过菜单[View|Assistant Editor]设置Assistant Editors的方向,例如On Right。

(2)呼叫辅助编辑窗口(option+)

Mac OS X日常操作中,我们已经认识到option这把单刀双掷开关的妙用:按住鼠标移动或三指触控移动时,按下option键可实现竖直块选调节音量/亮度时,按住 optionshift 可以四分之一单位微调

使用Xcode快捷键进行切换或跳转动作时,若同时按下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+Oshift+command+F(command+3)选中的文件或符号时,可同时按下option在辅助编辑窗口中打开。

control+1~6中打开选择结果时,均可同时按下option在辅助编辑窗口中打开。

(3)导航窗格(option+shift+)

在Xcode以上种种切换跳转操作时,按下option的同时按下shift,通常会呼出一个导航窗格(option+command+<),可选择在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 chooser prompts you to open the file in any open editor pane in any window and tab, or to open the file in a new editor pane, window, or tab.


6.环境变量(Build Setting Macros

(1)查看环境变量宏

命令行进入HelloWorld工程目录,执行xcodebuild命令并带上“-showBuildSettings”参数:

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(Mac OS X 10.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

以上是关于Xcode基本操作的主要内容,如果未能解决你的问题,请参考以下文章

是否可以恢复您上次在 UIwebView Xcode 5 中的位置

Xcode 6:按钮在所有设备上的图像视图中都停留在特定位置吗?约束?

echarts中颜色渐变写法

UIPageViewController - 双击点区域显示部分过渡(Xcode 8,Swift 3)

trim() 的秘密

Xcode 代码折叠与展开