Friday, October 8, 2010

Getting better at XCode

Yesterday i felt it was time to revisit the way i use XCode (3.2.4) and see if i can find some useful functionality that i can use more often. I also had a look at shortcuts and either created some that were missing or changed the ones that are hard to remember or simply hard to use for my taste.
Update: added sections Warnings, Split-Screen Editing, -(id) init {... and Recommended Reading [10/14/10]

File > Open Quickly... ⇧⌘D
This is probably the coolest feature i just haven't used yet. Instead of looking in the sidebar for your file and eventually clicking it, Open Quickly... opens up a little dialog window and shows matching files or methods as you type. I also realized that it captures your currently selected text.


Edit > Refactor...
[1]






I used the Refactor > Rename functionality occasionally but always had the impression that i'm waiting longer for it to finish as i would need changing the names by hand. But if you uncheck the Snapshot option it's actually quite fast so there's no excuse not to use it. I'm also going to use at least the option Extract from now on to extract lines of code to a new method if it makes sense.
Note: Please uncheck the Snapshot option at your own risk.

Edit > Completion List [2]
I probably never used this function a lot because my fingers aren't used to use any shortcuts containing Escape. I always used Next Completion instead but sometimes you aren't even sure about the first letter the function you need starts with so that's where Completion List is for.

Edit > Format > Re-Indent [3]
I realized that i'm re-indenting a lot by hand by throwing tabs and spaces at lines. This is stupid. Re-Indent can be used on a selection or on the current line. It also seems that ^I triggers Indent-Line.

Build > Clean ⇧⌘K
I actually never used this before... ok just kidding - but i always used the mouse to do this. The shortcut isn't that hard so i'll try to use that from now on.

Build > Build and Analyze ⇧⌘A

This is a somewhat useful thing to check on your alloc-fu. The clang analyzer will go through your code and will notice for instance that you don't release that object you created without autorelease.
There's also a target setting called "Run Static Analyzer" to run analyze automatically. Although it's a great way to keep writing alloc-sane code it slows down build time which is why i turned it off again.
Note: as suggested here i had to add -D__IPHONE_OS_VERSION_MIN_REQUIRED=040100
to Other-C-Flags in my project settings so that the analyzer works with my current XCode settings. Might be a specific problem related to 3.2.4.


Help > Quickhelp [4]
Wow i really feel stupid not to have used this function before. For example if you select a method name and execute Quickhelp it will show you a little help window with some documentation if available.

iPhone Simulator Window Scale Shortcuts
To change the iPhone Simulator's window size it seems you have to do it with the mouse curson in the menubar since there's no shortcut for it. I had a look at the XCode shortcuts but didn't find any setting for this.
So my solution was to create a shortcut under System Preferences > Keyboard.
Simply add an Application Shortcut for 50% and 100% and you are done.


Warnings
in the past weeks i noticed that i tend to care more about warnings. If you want to always see your warnings (they usually hide after you run the build a second time) just switch to the tab "All Results" in the "Build Results" Window.

Split-Screen Editing
Today i found out in the article mentioned below how to edit multiple files in one window.
There are two little icons on the right edge of the window above the scrollbar to add another window vertically or horizontally.

- (id) init { ...
If you are as lazy as i am and you didn't put a default init macro in your obj-c template file you can also insert a full init statement manually by using Edit > Insert Text Macro > Objective-C > init Definition. Shortcuts can be added via XCode Preferences.

Recommended Reading:
14 Essential Xcode Tips, Tricks and Resources for iPhone Devs by Dan Grigsby

[1] I assigned the shortcut ^R to it since i'm not going to use whatever ^R is usually doing.
[2] I assigned ^SPACE to it since that is close to the shortcut i know from eclipse.
[3] I assigned ⌥
⌘I for this.
[4] I didn't like the shortcut and now assigned ^H to it.

2 comments:

  1. Thanks for the article. I was just thinking I needed to get better with Xcode and this article helps. Especially the Build and Analyze, I'm really bad at not creating release statements that result in leaks so that's something I"m definitely going to use from now on.

    ReplyDelete
  2. Thanks for the comment, Shawn. I tend to forget my autorelease statements all the time so Build & Analyze has been a great help for me so far. Wish i had started using it earlier!

    ReplyDelete