XCode文档注释

使用Xcode的User Scripts可以很方便的为代码添加注释,方法如下

 XCode HeaderDoc

整行选中下面的代码,因为默认的Insert @method template脚本解析的问题,你需要保证+/-号后面有一个空格,否则插入注释时会出现问题。

+ (NSData *) captureScreenWithSize:(CGSize) captureSize atView:(UIView *)view;

选择HeaderDoc中的Insert @method template脚本,即可方便的插入脚本,插入代码大致如下

 XCode HeaderDoc Style

 

同样,我们可以生成其他代码注释,如header,class,protocol等。

我们可以修改默认的Insert @method template脚本,让其支持+ (NSData *)这样中间没有空格的情况。方法如下

选择Edit User Scripts…

 

Edit Users Scripts...

打开Insert @method template,将其中的代码替换为如下代码

 Insert @method template

#! /usr/bin/perl -w
#
# Inserts a template HeaderDoc comment for an
# Objective-C method.
# If the user selects a method declaration and
# chooses this command, the template includes
# the method name and the names of each parameter.
# If the user doesn't select a declaration before issuing
# this command, a default template is inserted.
 
use strict;
 
my $selection = <<'SELECTION';
%%%{PBXSelectedText}%%%
SELECTION
chomp $selection;
my $unmodifiedSelection = $selection; # used to retain linebreaks in output
 
$selection =~ s/\n/ /sg;     # put on one line, if necessary
$selection =~ s/\s+$//;      # remove any trailing spaces
$selection =~ s/\s{2,}/ /g;  # regularize remaining spaces
 
my $displayMethodName= '';
my $returnsAValue= 0;
my @params = ();
 
# is it a method declaration that we understand?
if (length($selection) && ($selection =~ /^[+-]/) && ($selection =~ /;$/)) {
    # determine if it returns a value
    $selection =~ m/[+-]\s*(\((.*?)\))?\s*(.*);/;
	my $return = $2;
	my $fullMethodName = $3;
	if ((defined($return)) && ($return ne 'void') && ($return ne 'IBAction')) {$returnsAValue=1;};
 
	if (defined($fullMethodName)) {
	    # get rid of type info for args
	    $fullMethodName =~ s/\(.*?\)//g;
 
		if ($fullMethodName =~ /:/) {
			# get keyword:arg pairs
			my @keyArgPairs = split(/\s+/, $fullMethodName);
 
			foreach my $pair (@keyArgPairs) {
				if ($pair =~ /:/) { # don't treat parameters with spaces as method names
				    my @parts = split(/:/, $pair);
				    while (@parts) {
					    $displayMethodName .= shift(@parts).":";
					    push (@params, shift @parts);
				    }
				} else {
				    if (length($pair)) { # but do add them to the parameter list
					push (@params, $pair);
				    }
				}
			}
		} else {
			$displayMethodName = $fullMethodName;
		}
	}
}
 
print "/*!\n";
print "    \@method     $displayMethodName\n";
print "    \@abstract   %%%{PBXSelection}%%%<#(brief description)#>%%%{PBXSelection}%%%\n";
print "    \@discussion <#(comprehensive description)#>\n";
 
foreach my $param (@params) {
	print "    \@param      $param <#(description)#>\n" if (defined($param));
}
 
print "    \@result     <#(description)#>\n" if ($returnsAValue);
print "*/\n";
print $unmodifiedSelection;
 
exit 0;

 另外,HeaderDoc中没有提供给property插入注释的代码, 我们可以加入如下代码来实现给property添加注释 Insert @property template

#! /usr/bin/perl -w
# Insert HeaderDoc template for class
# by xeonwell
use strict;
 
my $selection = <<'SELECTION';
%%%{PBXSelectedText}%%%
SELECTION
chomp $selection;
my $unmodifiedSelection = $selection;
$selection =~ s/\n/ /sg;     # put on one line, if necessary
$selection =~ s/\s+$//;      # remove any trailing spaces
$selection =~ s/\s{2,}/ /g;  # regularize remaining spaces
$selection =~ s/;$//;
 
my @params = split(/\s/, $selection);
my $pname = $params[scalar(@params)-1];
$pname =~ s/^\*//;
 
print "/*!\n";
print "    \@property $pname\n";
print "    \@abstract %%%{PBXSelection}%%%<#(brief description)#>%%%{PBXSelection}%%%\n";
print "*/\n";
print $unmodifiedSelection;
exit 0;

为了比较方便的插入文档注释, 我们还可以为这些脚本设置快捷键,当选中需要添加注释的method、property等时,直接按快捷键即可。

完成这些步骤之后,使用如下的方法来生成我们需要的注释

方法一:开启一个终端,然后切换到项目所在文件夹,运行如下命令

headerdoc2html –o hdoc *.h
gatherheaderdoc hdoc

方法二:使用doxygen来生成文档, 参考地址

http://www.stack.nl/~dimitri/doxygen/manual.html

参考文档:http://huajun.w18.net/?p=205

XeonWell Studio