iOS
Xcode Tips
June 9, 2009
0

More tips can be found in this other post: https://www.permadi.com/blog/2009/05/apple-xcode-shortcuts-and-tips/.

Creating Classes Diagram

There’s a handy feature to create class diagrams in Xcode, using Class Model feature.  To create a diagram for the whole project, select the project name in the Group & Files panel.

selectproject1

Then select Menu->Design->Quick Model->Class Model.  You will be asked to save the diagram.  Diagrams have extension of xcclassmodel, like ExampleProject_3.xcclassmodel.

classmodel1

You can also create diagram for individual class, by selecting a file in the Group & Folders panel and using the same menu.

Showing Line Number In Editor

Go to Menu->Xcode->Preferences. Select Text Editing, make sure Show line numbers is checked.

showlinenumber

Code Folding

Code folding is useful if you want to fold blocks of code (or a block), or if you have long code blocks and you want to see where it ends.  Move your mouse to the gutter on the left side of the editor, then Xcode with surround the currently selected block in a brighter color.codefolding

To fold block, click the mouse (while still on the gutter).  For example, below I folded the above function.

foldedcodeTo reopen a folded block, double-click the ellipsis or the right-arrow button on the left of the folded block.

The shortcut: Control+Command+up key folds and unfolds all code blocks.

Using NSLog to output to the Debug Console

NSLog is very handy to output to the debug console/gdb. It takes a string, and you can use NSString stringWithFormat: there.
stringWithFormat is similar to C’s printf, with some differences, the most major is to print an NSString, it uses %@ (you can still use %s to print regular strings).

For example:

NSLog([NSString stringWithFormat:@"My name is %@", @"My Name"]);

Outputs

My name is My Name.

You can also use standard C’s __LINE__ and __FILE__ on NSLog.

In addition, you can use special selector, named _cmd to get the name of the current method.

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
  // Configure and show the window
  [window addSubview:[navigationController view]];
  [window makeKeyAndVisible];
  NSLog(@"Log from %s Line %d", __FILE__, __LINE__);
}

The output is something like this:

Log from /ExampleProject/Classes/ExampleProjectAppDelegate.m Line 24

To open the debug console, use Run->Console menu.

To print method names, you can use NSStringFromSelector(_cmd) or the more familiar gcc’s __PRETTY_FUNCTION__ and __FUNCTION__ macros (references: http://www.delorie.com/gnu/docs/gcc/gcc_78.html).

For example:

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
  // Configure and show the window
  [window addSubview:[navigationController view]];
  [window makeKeyAndVisible];
  NSLog(@"Log fromn%s Line %d functionn%s", __FILE__, __LINE__, __PRETTY_FUNCTION__);
}

The output:

debugconsoleoutput

Quickly switching between .m and .h file

You can use option + command  + up key

Using Snapshots

Snapshots enables you to save the state of a project in a particular point of time. You can revert to the snapped state should you need to at a later time. You might be wondering why it is even needed, because you can do the same thing with Source Control Management such as SVN.  I can think of two reasons: a) Sometimes you have work-in-progress which you do not want to submit yet. b) Speed. Snapshot is quicker in some cases to take a snapshot than having to checkin and checkout changes.  It is also faster to do comparison on Snapshots.

snapshotmenu

To take Snapshots, select File->Make Snapshot.

To compare between two snapshots: highlight two Snapshots (shown below, left, the two grayed items), then select the file you want to compare (the yellow highlight below).  The differences will be shown in a split window.  In the example below, notice how the second revision shows the things that I added after the first snapshot (the word //Added log comment).snapshotdiffs

You can also revert the project to a previous snapshot, using the Restore toolbar buttons.