Easy Code Documentation with Xcode
Pages: 1, 2, 3
Adding Descriptions to the Documentation
It's nice to have a list of all our methods, but to really help our team we will have to write some good descriptions that will let them figure out how to use the methods. There are many different ways to do this with Doxygen, and you can find them in the Doxygen manual. These steps will get you started.
-
Let's start by editing DemoView.h. Open this file in the Xcode editor. It's often good to focus our documentation efforts on the header, as it describes the public interface to the class.
-
Notice that DemoView.h has two block comments at the beginning of the file. Let's modify the comments so that they show up in the class documentation. Add an extra asterisk (
*) to the opening of the comment. The first line will be:
/**
instead of:
/* -
Now remove the comment closer (
*/) from the first comment and the comment opener (/*) from the second comment so that all of the comment text at the beginning of the file is contained within one comment.Because our comment uses the double-asterisk opener and is at the top of the DemoView.h file, the contents of the comment will be used as the description for the DemoView class.
-
Build your project (press Cmd-B), and view the class documentation for DemoView to see the changes.
-
Let's add some more comments. Go back to the DemoView.h file and add the following line above the
int numline:/** Stores the type of shape to draw. 0=rectangles, 1=circles,2=bezierPaths, 3=circle clipping */Because the comment starts with the double asterisk and is directly before the
numdeclaration, this comment will be attached to thenumvariable in the documentation. -
Before the
setDemoNumbermethod declaration, add the following comment:/** Called by the interface to set the num variable @param newNum an integer specifying the kind of shape to draw. 0=rectangles, 1=circles, 2=bezierPaths, 3=circle clipping */Because this comment is directly above the
setDemoNumberfunction declaration, it will describe this function in the documentation. Also, notice that we were also able to describe a parameter by using the@paramsyntax. -
Build your project, and check out the documentation changes.

Before we conclude, I want to show you one more way of adding comments to the code. It often creates cleaner-looking code when a comment is on the same line as a variable declaration. For example, open the PathDemoController.h file in Xcode and find the following lines:
NSButton *button;
NSPopUpButton *popup;
NSWindow *window;
DemoView *demoView;
Change those lines of code to the following:
NSButton *button; /**< "Run Again" button in GUI */
NSPopUpButton *popup; /**< "Choose a path demo" popup in GUI */
NSWindow *window; /**< GUI window */
DemoView *demoView; /**< Custom view for displaying shapes */
By adding the > to the opening of the comment, we can use a comment that comes directly after the code that it refers to instead of placing the comment on the line above it. The documentation for Doxygen (or whatever document tool you choose to use) will have many little tricks like that, so be sure to check it out.
Final Thoughts
Automated API documentation for your code is easy to set up and even easier to maintain. Xcode can handle a lot of the repetitive work so that all you need to do is furnish some short descriptive comments as you maintain your code. Spend an hour or two evaluating the different documentation tools out there that work with your language of choice, and talk to your team about creating a policy for documenting code that way.
Something cool to consider is that because the documentation is integrated into the code, it works very well with a source control system such as CVS. If you check in your code for a daily build, that build could update the documentation on an intranet for all the developers. I'm interested (and I'm sure others are too) in learning how people are using automated code documentation in their real-world projects. I encourage you to leave a comment letting us know what your favorite documentation tools are and how you integrate them into your build system.
Happy documenting!
Resources
Adam Behringer works as the CEO of Bee Documents, which strives to help legal and medical firms efficiently manage their documents.
Return to MacDevCenter.com.
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 7 of 7.
-
Viewing documentation in Xcode
2004-09-21 09:16:05 flit [Reply | View]
You can view the documentation within Xcode if you set the filetype of the index.html file to text.html.documentation.
-
Separate target
2004-09-15 08:49:40 cparnot [Reply | View]
I created a separate target for the shell script: the target does not compile anything, it just runs the one shell script to build the documentation (I think in Xcode, it is called an aggregate target?). This way, I don't have Doxygen going through all the files in the source every time I build, and I can rebuild the documentation when I feel the need for it and/or I know I changed something. This makes a big difference for projects larger than the example here.
I have then a related question: is there a way to get Doxygen to be smarter and only scan files that have been modified? It seems it goes through all the files no matter what and rebuild the whole html directory from scratch every time it is called. -
Separate target
2006-03-24 01:34:56 RvA [Reply | View]
I did do this but I get two errors
/Volumes/Users/rva/Developer/PathDemo/build/PathDemo.build/Development/Documentation.build/Script-B3A7647709D3F1C0004FC3C0.sh: line 2: unexpected EOF while looking for matching `''
/Volumes/Users/rva/Developer/PathDemo/build/PathDemo.build/Development/Documentation.build/Script-B3A7647709D3F1C0004FC3C0.sh: line 3: syntax error: unexpected end of file
does some one know how to solve this?
I see this error even I do it step by step with the discussed settings.
I'm using XCode 2.2
Thanks -
Separate target
2006-03-24 02:02:59 RvA [Reply | View]
Shame on me!
Yesterday evening I was busy with this for a hour. checking the docs, the code, the files. Even spaces in the files their path.
Ahum, but not the path to the Doxygen application itself. There was a space in the path. After putting everything inside "" it runs like it should.
Hmm, was it coincedence? path problem <=> Pathdemo as the name :-)
-
If your are using AutoDoc,...
2004-08-30 11:21:41 i_iwas [Reply | View]
...check out AutoGraf and DocoaBrowser. Both are SourceForge hosted open source projects.
-
Why not AutoDoc?
2004-08-30 03:55:01 znek [Reply | View]
For the documentation of the ED frameworks we use AutoDoc from old MiscKit. The documentation looks quite like Apple's documentation. The documentation is automatically created via a post commit script, utilizing AutoDoc running in a GNUstep environment on FreeBSD 4.x.
-
yDoc
2004-08-28 12:30:31 BernhardGruber [Reply | View]
Adam writes great articles, I am looking forward to read his next WebObjects article.
For my projects I use yDoc http://www.yworks.com/en/products_ydoc.htm. It´s an extension to javadoc with quite impressive features like XML based custom tags, excluding specific methods or UML generation.





