Posts Tagged ‘Xcode’
Format Your Code With Crustify
The problem
Don’t you love your code? I mean, it is our product as developers. In the same way that a baker knows how to do and create delicious bread, we create source code…. beautiful source code. The code has to do its duty right and efficiently but it also has to be beautiful. There is nothing better that see your code after some time and feel that this is your code with the correct style.
There is another think to take into account. When we review a piece of code, if the code is not formatted in the way we expected we are adding lot of brain stress due to a non expecting format, avoiding we to focus properly on the most important, what the code does.
As you go coding you will make your way to the right format, because you will see tones of code on books, Apple examples, articles and more. But, there are tools that can help upfront.
Crustify
Yes, yes, I know you can format using XCode itself, but it is somehow limited. You can control some indexing and others, but if you want more, you can have a look to Crustify.
With a bit of initial effort, Crustify will give you full control on the code format with more than 400 settings to adjust. Here I will guide you throw the steps to set it up so you can tell by yourself.
Download and install it
So, lets focus. Go and download the source code from the Crustify site. There you will find our friend GIT, do not panic, use:
1 | git clone git://github.com/bengardner/uncrustify.git |
This will create a folder with all the code inside. The next step is to build it. Like the old days use:
1 2 | ./configure make |
On the “src” folder you will have the “crustify” executable. Move it to your “/usr/local/bin”, and you are done. You can test on the terminal somewhere with the command “crustify -h”.
Include in XCode
I can show how to run it on the command line, but I’m sure you want to go to the point and include it in XCode. Here we deal with Objective-C so first lets take the basic configuration that comes with the source code. Take the file “etc/objc.cfg” and save it where ever you think is right (your configurations folder maybe!). It contains a default configuration good enough to start with.
This configuration file though is loosely documented so it is mandatory you to read the 70 lines file “documentation/htdocs/configuration.txt” to understand some of its simple enough syntax.
If you want to explore further the effect of every setting in your code you can download the UniversalIndentGUI. There, select Crustify and you will be able to, graphically, play with the setting so you get the best of the tool.
Better if you go adjusting your configuration as you go, so let’s set up and easy way to run Crustify from XCode. I borrow the small script from Scott from HackerToys
1 2 3 4 | #!/bin/sh echo -n "%%%{PBXSelection}%%%" /path/to/uncrustify -q -c /path/to/defaults.cfg echo -n "%%%{PBXSelection}%%%" |
The only thing we need to do is add a new user script in XCode as shown on the picture (use you own path).

You may assign a shortcut if you use it very often. Now you are done. Just open any file and run the script using the script menu of XCode.
Enjoy your coding!
I need to say this again, the code is your product so… make it shine!!
Undo and Redo
This topic is quite difficult to digest, but after few exercises you should be able to manage it.
When you work with an application, it usually happens that, after applying some edits to your data, you realize that the final result is not what you were expecting. So, you wish to go back and remove what you have done. I believe you are familiar with the Undo and Redo items in the Edit menu of your Mac. But, how can you implement these functionalities in your application?
Cocoa and Cocoa Touch use an intelligent approach to manage this. To understand it, I have to introduce the concept of NSInvocation. An NSInvocation is an object containing all the elements of an Objective-C message: a target, a selector, some arguments, and the return value. Each of these elements can be set directly and the return value is set automatically when the NSInvocation object is dispatched. NSInvocation works together with the NSUndoManager to make the Undo/Redo mechanism possible.
When you apply an edit onto your data and you want to be able to undo that edit, you need to prepare (coding) the inverse operation corresponding to that particular edit. Then, you pack this inverse message (including selector, arguments and return value) in an NSInvocation object and forward it to the NSUndoManager. This will store this object in a stack and wait for the user action.
Indeed, there are two stacks: the Undo Stack and the Redo Stack and their scope is very similar. The management of the two stacks is assigned to NSUndoManager and you don’t need to take care of it. When you undo some operation, NSUndoManager removes from the Undo Stack that operation and adds it to the Redo Stack. If you redo that operation, this time the NSUndoManager removes it from the Redo Stack and adds it to the Undo Stack.
Let’s look at the details and how to code it. Suppose you are writing a simple calculator with a plus and minus operations:
1 2 | - (NSNumber *)add:(NSNumber *)aNumber to:(NSNumber *)anotherNumber; - (NSNumber *)subtract:(NSNumber *)aNumber from:(NSNumber *)anotherNumber; |
You want to be able to undo and redo each of these operations after you apply them to your data. When you start your application, an NSUndoManager object is created for you with no costs. In reality, if you are writing a document-based application in Cocoa, an NSUndoManager is created automatically. In the other cases (iPhone included), you have to create it explicitly.
You usually need only an NSUndoManager for each application. To register the above two methods to the NSUndoManager mechanism, you do the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | - (NSNumber *)add:(NSNumber *)aNumber to:(NSNumber *)anotherNumber { NSUndoManager *undo = [self undoManager]; [[undo prepareWithInvocation] subtract:aNumber from:anothernumber]; float F1 = [aNumber floatValue]; float F2 = [anotherNumber floatValue]; F1 += F2; return [NSNumber numberWithFloat]; } - (NSNumber *)subtract:(NSNumber *)aNumber from:(NSNumber *)anotherNumber { NSUndoManager *undo = [self undoManager]; [[undo prepareWithInvocation] add:aNumber to:anotherNumber]; float F1 = [aNumber floatValue]; float F2 = [anotherNumber floatValue]; F1 -= F2; return [NSNumber numberWithFloat]; } |
The undoManger method (lines 3 and 4) is defined in the NSDocument, NSResponder, NSManagedObjectContext and WebView classes. Please, refer to the documentation of these classes for details.
The trick is at the lines 4 and 15. There, you register to the Undo/Redo mechanism the opposite operation to your current edit.
You can also change the label of the Undo and Redo operation using the
1 | - (void)setActionName:(NSString *)actionName; |
So, you can add this line after line 4:
1 | [undo setActionName:@"Add"]; |
Once implemented, you will have in the edit menu “Undo Add”.



