您的位置:首页 > Web前端

Hidden Features of Xcode

2013-07-08 09:03 381 查看


Hidden Features of Xcode


Hidden Features
of Xcode(http://stackoverflow.com/questions/146297/hidden-features-of-xcode)






338favorite

1047

With a huge influx of newbies to Xcode, I'm sure there are lots of Xcode tips and tricks to be shared.

What are yours?

objective-c xcode osx cocoa
share
edited May
6 '12 at 17:23

community
wiki

11
revs, 8 users 52%

Robert Harvey


locked by Bill
the Lizard♦ Oct
5 '11 at 13:37

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use
it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: Help
center.


89 Answers

activeoldestvotes

123next

343


Switch to Header/Source File

Option ⌥ Command ⌘ Up Arrow ↑

View > Switch to Header/Source File

Switches between the
.m
and
.h
files.

In Xcode 4 this is ctrl Command ⌘ Up Arrow ↑

share
edited Feb
19 '11 at 12:16

community
wiki

8
revs, 3 users 72%

Senseful

7
This is absolutely my favorite, too. I use it constantly. Also works when we are talking about cpp and h. Not just m! :) – que
que Sep
30 '08 at 21:20
24
It's called "option" on the Mac, not "alt". And if you want fancy symbols, it can be written ⌥⌘↑ – Brian
Campbell Mar
31 '09 at 15:33
22
@Brian Your right...but it also says "alt" on the key – epatel Mar
31 '09 at 18:44
4
Note that in Xcode 3.2, you have to change the key bindings to restore ⌘⌥⇠/⇢ to switch-file. They changed the default to move between
positions in the same file. – Peter
Hosey Sep
14 '09 at 12:34
71
A three finger swipe up on the touchpad is another shortcut for the same action. – Nathan Dec
30 '09 at 2:07
show 15 more
comments





158
⌘ Command +
Double-Click
on a symbol
: Jump to Definition of a symbol.

⌥ Option +
Double-Click
on a symbol
: Find Text in Documentation of a symbol. (Only works if you have they symbol's Doc Set installed.)


Favorites Bar:

Favorites bar is just like you have in Safari for storing - well - favorites. I often use it as a place to store shortcuts (which you can do by drag-dropping) to files I am using right now. Generally this is more useful when I'm working with a large or unfamiliar
project.

To show the Favorites Bar, select the following menu option:

View
>
Layout
>
Show
Favorites Bar


share
edited Apr
4 '11 at 11:46

community
wiki

7
revs, 4 users 60%

schwa

11
Gasp! Show Favorites Bar, where have you been my whole life! – willc2 Jun
28 '09 at 5:14
4
The favorites bar has been "replaced" in Xcode 4 with persistent tabs. – Ascendant Aug
5 '11 at 5:35
show 5 more
comments
127


Auto-completion Keyboard Shortcuts

Tab ⇥ OR Control ⌃ /: Select the next auto-completion argument.

Shift ⇧ Tab ⇥ OR Shift ⇧ Control ⌃ /: Select the previous auto-completion argument.

Escape ⎋: Shows the auto completion pop-up list.

share
edited Aug
15 '11 at 22:26

community
wiki

5
revs, 3 users 48%

Senseful

18
Note that the new XCode uses Tab to move between arguments in completions. It's more fluid. – Jab Aug
17 '09 at 17:48
show 8 more
comments
119


Turn off the "undo past the last point" warning

When you attempt to undo after saving, you will get the following prompt:

"You are about to undo past the last point this file was saved. Do you want to do this?"

To get rid of this warning, enter the following into a terminal window:
defaults write com.apple.Xcode XCShowUndoPastSaveWarning NO



Change the company name in template files

Paste this into the Terminal application:
defaults write com.apple.Xcode PBXCustomTemplateMacroDefinitions '{"ORGANIZATIONNAME" = "Microsoft";}'



Change "
com.yourcompanyname
"
in all your templates:

Find the directory:
/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Project
Templates/Application


Use your favourite multi-file search-and-replace tool to change
com.yourcompany
to
whatever value you normally use to build for a device. I used BBEdit's multi-find-and-replace after I opened the whole directory. You should be replacing the value in all the
info.plist
files.
I found 8 files to change. The number of times a build has failed because I forgot to change this string is ridiculous.


Quickly jump to a Group in the Groups and Files pane

Control ⌃ Option ⌥ Shift ⇧ + <First letter of a Group name>

If you hold down the three keys above, you can quickly jump to groups in the left (Groups and Files) page by pressing the first letter of a groups name. For example, Control ⌃Option ⌥Shift ⇧T takes
you to Targets and Control ⌃Option ⌥Shift ⇧S to Source.
Press it again and it jumps to SCM. Sometimes it takes several tries for this to work (I don't know why).


Cycling between autocompletion choices

Control ⌃ .

Shift ⇧ Control ⌃ .: Cycles backwards between autocompletion choices.

Control ⌃. (Control-Period) after a word automatically accepts the first choice from the autocompletion menu. Try typing
log
then Control
⌃. and you'll get a nice
NSLog
statement.
Press it again to cycle through any choices. To see all the mutable choices, type
NSMu
then Control
⌃..


Quick Help

Control ⌃ Command ⌘ ? (While your cursor is in the symbol to look up)

Option ⌥ + <Double-click a symbol>

Help > Quick Help

To get to the documentation from the Quick Help window, click the book icon on the top right.


See the documentation for a symbol

Command ⌘ Option ⌥ + <Double-click a symbol>

Takes you straight to the full documentation.


Make non-adjacent text selections

Command ⌘ Control ⌃ + <Double-click in the editor>

Use the above shortcut for a strange way of selecting multiple words. You can make selections of words in totally different places, then delete or copy them all at once. Not sure if this is useful. It's Xcode only as far as I can tell.


Use Emacs key bindings to navigate through your code

This trick works in all Cocoa application on the Mac (TextEdit, Mail, etc.) and is possibly one of the most useful things to know.

Command ⌘ Left Arrow or Command ⌘ Right Arrow Takes you to the beginning and end of a line.

Control ^ a and Control ^ e Do the same thing

Control ^ n and Control ^ p Move the cursor up or down one line.

Control ^ f and Control ^ b Move the cursor back or forward one space

Pressing Shift ⇧ with any of these selects the text between move points. Put the cursor in the middle of a line and press Shift ⇧ Control ^ e and you can select to the end of the
line.

Pressing Option ⌥ will let you navigate words with the keyboard. Option ⌥ Control ^ f skips to the end of the current word. Option ⌥ Control ^ b skips
to the beginning of the current word. You can also use Option ⌥ with the left and right arrow keys to move one-word-at-a-time.

Control ^ Left Arrow and Control ^ Right Arrow moves the cursor between camel-cased parts of a word.

Try it with
NSMutableArray
.
You can quickly change it to
NSArray
by
putting your cursor after the
NS
,
pressing Shift ⇧ Control ^ Right Arrow then Delete.

share
edited Oct
22 '11 at 16:54

community
wiki

9
revs, 3 users 51%

nevan king

4
Control-Command-double-click is just necessary because Xcode uses Command-double-click for something else other than a discontiguous
word selection. In most Cocoa apps you can do discontiguous selection with the Command modifier alone. – Nicholas
Riley Nov
1 '09 at 1:26
6
+1 for org name Microsoft – slf Apr
21 '10 at 12:29
show 3 more
comments
99


Open Quickly...

Command ⌘ Shift ⇧ D

File > Open Quickly...

I'm a big fan of the Open Quickly feature, which is particularly good in Xcode 3.1 and later. When you want to open a file or a symbol definition
that's in your project or in a framework, just hit the keyboard shortcut, type a bit of the file or symbol's name, use Up Arrow ↑ and Down Arrow ↓ to pick to the right result (if need be), and then hit Return
↩ to open the file or navigate to the symbol definition.

On Xcode 4:

Command ⌘ Shift ⇧ o


Open Quickly uses the current word as a search term

Also, something I didn't know about Xcode until two minutes ago (when schwa pointed
it out in a comment) is that, if the editor's text caret is inside of a word when Open Quickly is invoked, that word will be used as the Open
Quickly search term.

share
edited Apr
4 '11 at 12:37

community
wiki

7
revs, 3 users 62%

Senseful

3
a.k.a. your cursor. – willc2 Jun
28 '09 at 5:15
9
On the Mac, a "cursor" indicates your mouse position; the flashing vertical bar is the "insertion point". – Nicholas
Riley Nov
1 '09 at 1:22
1
@Nicholas Riley: Caret is the right word, AFAIK - our mac developers always called it that when I worked in a software company – Flubba May
9 '10 at 19:17
3
@Flubba: Caret is certainly used for
this, I don't dispute it at all; it's just not the common user- or developer-centric term on Macs. If you Google for "caret site:developer.apple.com" you'll just find hits in TrueType documentation and in a single, very recently written, iPad document. – Nicholas
Riley May
9 '10 at 21:32
show 6 more
comments
79


Use #pragma for organization

You can use:
#pragma mark Foo


... as a way to organize methods in your source files. When browsing symbols via the pop up menu, whatever you place in
Foo
will
appear bold in the list.

To display a separator (i.e. horizontal line), use:
#pragma mark -


It's very useful, especially for grouping together delegate methods or other groups of methods.

share
edited Jun
22 '10 at 19:50

community
wiki

2
revs, 2 users 67%

Steve Streza

9
typing # p will usually trigger code completion '#pragma mark <LABEL>' for quickly adding the label part. – willc2 Jun
28 '09 at 5:22
1
It also grabs
#
TODO
statements. Now if only it would also grab
#
XXX
, which is commonly used in the code base I work on... – asmeurer Dec
28 '10 at 6:22
7
Xcode 4 also supports "#pragma mark - Foo" which adds the mark AND a separator at the same time. – typeoneerror Aug
4 '11 at 0:07
77


1. Breakpoint on "objc_exception_throw"

You should always have a breakpoint
on
objc_exception_throw
.


2. Debugging retain/release problems with "Zombie" variables

Use the following code:
NSZombieEnabled = YES;
NSDeallocateZombies = NO;


... to debug retain and release problems. For more information, see the "Finding Memory Leaks" section of Apple's
Debugging Applications document.


3. Jumping to a class in Xcode from Interface Builder

Command ⌘ +
Double-click
on
an object in Interface Builder's Document Window to jump to that class in Xcode. This is very handy with File's Owner.


4. Reusing customized objects in Interface Builder

Drag a customized object back to Interface Builder's Library for later reuse.


5. Select overlapping items in Interface Builder

Control ⌃ Shift ⇧ +
Click
on
an object in Interface Builder to see a menu of all of the objects under the mouse.


6. Interface Builder Gesture Guide

Interface
Builder Gesture Guide.

share
edited Aug
15 '11 at 22:25

community
wiki

6
revs, 3 users 75%

Senseful

3
Great Answer! I'd give two upvotes for (3) and (4) which I didn't know before. – Nikolai
Ruhe Sep
22 '09 at 7:58
show 1 more
comment
75
Ctrl + 2: Access the popup list of methods and symbols in the current file.

This is super useful because with this shortcut you can navigate through a file entirely using the keyboard. When you get to the list, start typing characters and the list will type-select to the symbol you are looking for.

share
edited Dec
10 '10 at 16:17

community
wiki

4
revs, 4 users 38%

Senseful

19
I use Spaces (CTRL-2 goes to Space #2) so I remapped the key binding. It can be found under "Text Key Bindings" in item "Pop Symbols
PopUp." – bbrown Apr
10 '09 at 21:16
1
Nice tip. Made me discover that Control-1 will bring up the file history list. – tmadsen Feb
8 '10 at 7:28
2
In XCode5 it's CTRL+6 I believe – Ran Apr
5 '11 at 5:59
1
Is there a keybinding for this in XCode 4? I'm using Spaces, so I'd like to rebind this, but I can't find it. – Geoffrey
Wiseman Apr
8 '11 at 1:41
5
In XCode4 it's ^6. It's called Show Document Items. – WBlasko Jul
5 '11 at 2:02
show 2 more
comments
74


Zoom Editor In

If your window displays both the detail and editor view, you can zoom the editor in to expand the editor view to the full height of the window. (This
is fairly easily found, but many seem to overlook it.)

You can do this by using one of the following methods:

Command ⌘ Shift ⇧ E

View > Zoom Editor In

Drag the splitter (between the editor window and the file list above it) upwards.

share
edited Aug
15 '11 at 22:21

community
wiki

5
revs, 2 users 73%

Senseful

18
It took me AGES to find out how to hide the tree as well - Command-Option-Shift-E. Ah, sweet relief. I'm forever indebted to the MacMacDev
Glasgow group for letting me know this. – John
Gallagher Oct
12 '09 at 9:01
67
Get Colin
Wheeler's Complete Xcode Keyboard Shortcut List (available as PDF or PNG). Print it and keep it somewhere visible (I've got it on the wall next to my screen).

edit: Updated
versions for Xcode 3.2

edit 2: Updated
versions for Xcode 4

share
edited Mar
12 '11 at 22:01

community
wiki

3
revs

Hagelin

52
Not much of a keyboard shortcut but the TODO comments in the source show up in the method/function dropdown at the top of the editor.

So for example:
// TODO: Some task that needs to be done.


shows up in the drop down list of methods and functions so you can jump to it directly.

Most Java IDEs show a marker for these task tags in the scrollbar, which is nicer, but this also works.

share
answered Oct 14 '08
at 4:33

community
wiki

comctrl6

11
This even works with // !!!: and // ???: – Jens
Kohl Sep
19 '09 at 23:39
13
Works for //FIXME: too. – ustun Oct
3 '09 at 11:06
4
I just tried // MARK: and it has the exact same effect as #pragma mark. You can even use // MARK: with a dash to get the separator. – Rose
Perrone Sep
2 '10 at 4:12
9
Anyone know how to do this in Xcode4? – ing0 Apr
6 '11 at 13:16
5
In Xcode 3 TODO comments can be placed anywhere in the code and they will appear in the function popup. For some reason in Xcode 4
TODO comments must be outside any function/method body to appear. It's a bug. – SteveCaine Sep
27 '11 at 20:31
show 3 more
comments
51
⌘-[ and ⌘-] to indent and unindent selected text. Makes cleaning up source code much easier.

share
answered Nov 8 '08
at 22:31

community
wiki

Michel

14
Or just select it and hit control-I (like tab but not really). – Nicholas
Riley Nov
1 '09 at 1:35
show 3 more
comments
43
Hold Option while splitting windows to split vertically rather than horizontally.

share
answered Mar 31 '09
at 15:21

community
wiki

Mike

4
Very nice on a 30" monitor. Thanks! – Rob Sep
14 '09 at 2:55
1
This helped a lot! – Raj May
18 '10 at 12:47
show 1 more
comment
42
Double-click on the square brackets or parentheses to obtain bracket and parentheses matching.

share
answered Jan 15 '09
at 20:25

community
wiki

Julien
Chastang

show 1 more
comment
36
Control Xcode's text editor from the command line: xed
> xed -x                # open a new untitled document
> xed -xc foo.txt       # create foo.txt and open it
> xed -l 2000 foo.txt   # open foo.txt and go to line 2000

# set Xcode to be your EDITOR for command line tools
# e.g. for subversion commit
> echo 'export EDITOR="xed -wcx"' >> ~/.profile

> man xed               # there's a man page, too


share
edited Oct
22 '11 at 16:54

community
wiki

2
revs, 2 users 90%

Nikolai Ruhe

show 2 more
comments
31
"Ctrl+Left/Right Arrow" to do intra-word text navigation. I use this feature to jump the cursor from the one "camel hump" in a variable to the next.

share
answered Oct 1 '08
at 14:41

community
wiki

Matt
Dillard

10
It's great so long as you have Spaces disabled :\ – jbrennan Aug
17 '09 at 17:58
2
@jbrennan I have Spaces assigned to ⌃+⌥+⇧+⌘ Edit: To set Spaces to ⌃+⌥+⇧+⌘, select the "To switch between spaces:" and the "To switch
directly to a space:" popup and hold down the Control, Option, Shift and Command keys. – jrtc27 Jul
12 '10 at 9:51
show 4 more
comments
30
Xcode supports text macros that can be invoked via the Insert Text Macro menu at the end of the Editmenu.
They can also be invoked using Code Sense, Xcode's code completion technology.

For example, Typing the key sequence
p
i m control-period
will insert
#import
"file"
into your code, with
file
as
an editable token just like with code completion.

share
edited Oct
1 '08 at 8:17

community
wiki

2
revs

Chris Hanson

show 1 more
comment
30
Right click on a variable in your function and click edit all in scope. Been using it a lot since I found this out.

ctrl ⌘ T

share
edited Nov
27 '10 at 13:47

community
wiki

2
revs, 2 users 67%

robottobor

7
You can access this using Ctrl-Command-T when over the word you want to edit. Much faster than a pesky menu! – John
Gallagher Nov
27 '09 at 17:23
3
The default shortcut changed to Ctrl+Command+E in Xcode 4, and can be changed in the key bindings preferences. – Dov Apr
13 '11 at 19:28
show 2 more
comments
28
You can have Xcode run the preprocessor over your Info.plist file:
<key>CFBundleShortVersionString</key>
#ifdef DEBUG
<string>1.0 (debug)</string>
#else
<string>1.0</string>
#endif


See http://developer.apple.com/technotes/tn2007/tn2175.html for
details.

share
answered Sep 30 '08
at 18:25

community
wiki

Dewayne
Christensen

16
Note that if you do this, your Info.plist will always have to be edited as text; you won't be able to edit it in the nice Property
List Editor that keeps it using correct keys and value types. – Chris
Hanson Oct
1 '08 at 7:46
27
Debugging - how to use GDB

Being new to this still, I find trapping and identifying faults a rather daunting job. The console, despite it being a powerful tool, usually does not yield very intuitive results and knowing what you are looking at in the debugger can be equally difficult
to understand. With the help of some of they guys on Stack Overflow and the good article about debugging that can be found at Cocoa
With Love it becomes a little more friendly.

share
edited Oct
22 '11 at 16:49

community
wiki

2
revs, 2 users 67%

Joe

1
Wow, that's a great article. – Athena Dec
13 '08 at 16:26
26
Navigate among open files back and forth:

⌥⌘←

⌥⌘→

share
edited Nov
1 '09 at 1:35

community
wiki

2
revs, 2 users 67%

Vladimir Grigorov

5
The key is "option" (⌥) on the Mac, not "alt." – Chris
Hanson Oct
4 '08 at 19:14
7
The white Apple keyboard does indeed have alt written on the option key. – Chris
Lundie Oct
19 '08 at 2:35
19
Chris, the 'alt' label is for switchers. Old school Mac guys know it as Option. Like God intended. – willc2Jun
28 '09 at 5:18
1
If you're a fan of trackpad gestures, you can also use a three-finger swipe to the left and right to get the same effect. – Reed
Olsen Aug
27 '10 at 17:46
show 1 more
comment
25
Technically an Interface Builder tip, but they're a book-matched pair, so I don't think this is off topic...

Shift + Right Click on one of your controls and you get a nice pick list of the object hierarchy. No more click, click, click, frustration!

share
answered May 16 '09
at 7:57

community
wiki

blissapp

22
With Trackpad:

Swipe Three Fingers Up - Switch between header and source file, which is easier than Cmd + Opt + Up;

Swipe three fingers down - Switch between declaration and definition when selecting a class or method, found these two kind currently;

Swipe three fingers left - Go back (Cmd + Opt + Left);

Swipe three fingers right - Go forward (Cmd + Opt + Right);

Tested with Xcode 3.2.5.

share
edited Oct
22 '11 at 10:15

community
wiki

4
revs, 2 users 73%

iwill

21
The class browser in Xcode! Reached by pressing shift + ⌘ + c.
You can reduce the scope to only show your active project. It gives you a less cluttered view as long as you only want to browse the class hierarchy.

share
edited Dec
10 '10 at 16:23

community
wiki

3
revs, 2 users 75%

cschreiner

show 4 more
comments
19


Using ] to automatically insert
[
in
the correct location

I come from a .NET background, so I'm used to typing a symbol and then typing one of its method names. So I always forget to include the
[
before
I start typing the object name. Usually this meant I would need to go to the beginning of the line and add the
[
manually.
I didn't realize I could just press ] at the current cursor position, and it will be added automatically.

There are ways to use this: either after typing the function's name, or right before typing the function's name.


Method 1: after the function name

myObject
testMethod
]

... becomes:
[myObject testMethod]


... with the caret positioned after the
]
.


Method 2: before the function name

myObject
]

... becomes:
[myObject ]


... with the caret positioned right before the
]
.

The advantage of the latter (2) is that code completion will filter on the methods of your object. Whereas with the former (1) if you try to invoke code completion immediately after
myObject
,
it won't be filtered. Another advantage to (2) is it behaves more like other programming languages that use dot notation. You type the name of the object then simply ] instead of . to access a method.

share
edited Oct
22 '11 at 17:02

community
wiki

4
revs, 2 users 95%

Senseful

1
thanks - this is quite handy! – Faisal
Vali Oct
23 '10 at 15:03
17
Select a block of text and type cmd-/ to comment it out. Do it again to remove the comments characters.

This is especially useful when combined with brace-matching by double-clicking on balanced chars (parens, braces, brackets).

share
answered Apr 10 '09
at 18:26

community
wiki

Frank
Szczerba

show 1 more
comment
16
Being able to quickly see all the methods that can be overriden from a super class. For example when extending UITableViewController I just type in my implementation:
- ta


and then I hit ESC to see all the methods from my superclass that begin with "ta" such as
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath


This also works when adopting protocols.

share
answered Aug 18 '10
at 4:07

community
wiki

nicktmro

14
Command ⌘ alt ⌥ shift T : reveal the current edited file in the project tree.

share
edited Oct
7 '10 at 13:04

community
wiki

2
revs, 2 users 67%

VdesmedT

show 2 more
comments
14
ctrl + alt + ⌘ + r to clear the log

share
edited Mar
29 '11 at 18:51

community
wiki

2
revs, 2 users 67%

Nick

1
Interesting, I thought Apple frowned on shortcuts that needed both hands to execute? – Tejaswi
Yerukalapudi Apr
4 '11 at 15:03
2
Xcode 4 ⌘ + K – Ahmad
Kayyali Oct
10 '11 at 14:14
13


Use the Class Browser to show inherited methods

Apple's API reference documentation does not show methods inherited from a superclass. Sometimes, though. it's useful to be able to see the full range of functionality available for a class -- including a custom class of your own. You can use the Class Browser
(from the Project menu) to display a flat or hierarchical list of all the classes related to a current project. The upper pane on the right hand side of the browser window shows a list of methods associated with the object selected in the browser. You can
use the Configure Options sheet to select "Show Inherited Members" to show inherited methods as well as those defined by the selected class itself. You click the small book symbol to go to the corresponding documentation.

share
answered Oct 22 '08
at 18:21

community
wiki

mmalc

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: